You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gitea/models/user.go

321 lines
8.2 KiB

11 years ago
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
11 years ago
package models
import (
"errors"
"fmt"
"os"
"path/filepath"
11 years ago
"strings"
"time"
"github.com/dchest/scrypt"
"github.com/gogits/git"
"github.com/gogits/gogs/modules/base"
11 years ago
)
// User types.
11 years ago
const (
11 years ago
UT_INDIVIDUAL = iota + 1
UT_ORGANIZATION
11 years ago
)
11 years ago
// Login types.
11 years ago
const (
11 years ago
LT_PLAIN = iota + 1
LT_LDAP
11 years ago
)
// User represents the object of individual and member of organization.
11 years ago
type User struct {
Id int64
LowerName string `xorm:"unique not null"`
Name string `xorm:"unique not null"`
Email string `xorm:"unique not null"`
Passwd string `xorm:"not null"`
11 years ago
LoginType int
Type int
NumFollowers int
NumFollowings int
NumStars int
NumRepos int
Avatar string `xorm:"varchar(2048) not null"`
AvatarEmail string `xorm:"not null"`
Location string
Website string
IsActive bool
Rands string `xorm:"VARCHAR(10)"`
Expired time.Time
11 years ago
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
// HomeLink returns the user home page link.
func (user *User) HomeLink() string {
return "/user/" + user.LowerName
}
// AvatarLink returns the user gravatar link.
func (user *User) AvatarLink() string {
return "http://1.gravatar.com/avatar/" + user.Avatar
}
11 years ago
type Follow struct {
Id int64
UserId int64 `xorm:"unique(s)"`
FollowId int64 `xorm:"unique(s)"`
Created time.Time `xorm:"created"`
}
var (
ErrUserOwnRepos = errors.New("User still have ownership of repositories")
11 years ago
ErrUserAlreadyExist = errors.New("User already exist")
ErrUserNotExist = errors.New("User does not exist")
ErrEmailAlreadyUsed = errors.New("E-mail already used")
11 years ago
)
11 years ago
// IsUserExist checks if given user name exist,
// the user name should be noncased unique.
11 years ago
func IsUserExist(name string) (bool, error) {
return orm.Get(&User{LowerName: strings.ToLower(name)})
}
// IsEmailUsed returns true if the e-mail has been used.
func IsEmailUsed(email string) (bool, error) {
return orm.Get(&User{Email: email})
}
// NewGitSig generates and returns the signature of given user.
11 years ago
func (user *User) NewGitSig() *git.Signature {
return &git.Signature{
Name: user.Name,
Email: user.Email,
When: time.Now(),
}
}
// return a user salt token
func GetUserSalt() string {
return base.GetRandomString(10)
}
// RegisterUser creates record of a new user.
func RegisterUser(user *User) (*User, error) {
isExist, err := IsUserExist(user.Name)
11 years ago
if err != nil {
return nil, err
11 years ago
} else if isExist {
return nil, ErrUserAlreadyExist
11 years ago
}
isExist, err = IsEmailUsed(user.Email)
if err != nil {
return nil, err
} else if isExist {
return nil, ErrEmailAlreadyUsed
}
user.LowerName = strings.ToLower(user.Name)
user.Avatar = base.EncodeMd5(user.Email)
user.AvatarEmail = user.Email
user.Expired = time.Now().Add(3 * 24 * time.Hour)
user.Rands = GetUserSalt()
if err = user.EncodePasswd(); err != nil {
return nil, err
} else if _, err = orm.Insert(user); err != nil {
return nil, err
} else if err = os.MkdirAll(UserPath(user.Name), os.ModePerm); err != nil {
if _, err := orm.Id(user.Id).Delete(&User{}); err != nil {
return nil, errors.New(fmt.Sprintf(
"both create userpath %s and delete table record faild: %v", user.Name, err))
}
return nil, err
}
return user, nil
11 years ago
}
11 years ago
// UpdateUser updates user's information.
func UpdateUser(user *User) (err error) {
_, err = orm.Id(user.Id).Update(user)
11 years ago
return err
}
11 years ago
// DeleteUser completely deletes everything of the user.
func DeleteUser(user *User) error {
// Check ownership of repository.
count, err := GetRepositoryCount(user)
if err != nil {
return errors.New("modesl.GetRepositories: " + err.Error())
} else if count > 0 {
return ErrUserOwnRepos
}
// TODO: check issues, other repos' commits
// Delete all feeds.
if _, err = orm.Delete(&Action{UserId: user.Id}); err != nil {
return err
}
// Delete all SSH keys.
keys := make([]PublicKey, 0, 10)
if err = orm.Find(&keys, &PublicKey{OwnerId: user.Id}); err != nil {
return err
}
for _, key := range keys {
if err = DeletePublicKey(&key); err != nil {
return err
}
}
11 years ago
// Delete user directory.
if err = os.RemoveAll(UserPath(user.Name)); err != nil {
return err
}
_, err = orm.Delete(user)
11 years ago
// TODO: delete and update follower information.
return err
}
// EncodePasswd encodes password to safe format.
func (user *User) EncodePasswd() error {
newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte(base.SecretKey), 16384, 8, 1, 64)
11 years ago
user.Passwd = fmt.Sprintf("%x", newPasswd)
11 years ago
return err
}
// UserPath returns the path absolute path of user repositories.
func UserPath(userName string) string {
11 years ago
return filepath.Join(RepoRootPath, strings.ToLower(userName))
}
func GetUserByKeyId(keyId int64) (*User, error) {
user := new(User)
rawSql := "SELECT a.* FROM user AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?"
if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" {
rawSql = "SELECT a.* FROM \"user\" AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?"
}
has, err := orm.Sql(rawSql, keyId).Get(user)
if err != nil {
return nil, err
} else if !has {
err = errors.New("not exist key owner")
return nil, err
}
return user, nil
}
// GetUserById returns the user object by given id if exists.
func GetUserById(id int64) (*User, error) {
user := new(User)
has, err := orm.Id(id).Get(user)
if err != nil {
return nil, err
}
if !has {
return nil, ErrUserNotExist
}
return user, nil
}
// GetUserByName returns the user object by given name if exists.
func GetUserByName(name string) (*User, error) {
if len(name) == 0 {
return nil, ErrUserNotExist
}
user := &User{
LowerName: strings.ToLower(name),
}
has, err := orm.Get(user)
if err != nil {
return nil, err
} else if !has {
return nil, ErrUserNotExist
}
return user, nil
}
11 years ago
// LoginUserPlain validates user by raw user name and password.
11 years ago
func LoginUserPlain(name, passwd string) (*User, error) {
user := User{LowerName: strings.ToLower(name), Passwd: passwd}
if err := user.EncodePasswd(); err != nil {
11 years ago
return nil, err
}
has, err := orm.Get(&user)
if err != nil {
return nil, err
} else if !has {
err = ErrUserNotExist
11 years ago
}
return &user, err
11 years ago
}
11 years ago
// FollowUser marks someone be another's follower.
func FollowUser(userId int64, followId int64) (err error) {
11 years ago
session := orm.NewSession()
defer session.Close()
session.Begin()
if _, err = session.Insert(&Follow{UserId: userId, FollowId: followId}); err != nil {
11 years ago
session.Rollback()
return err
}
rawSql := "UPDATE user SET num_followers = num_followers + 1 WHERE id = ?"
if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" {
rawSql = "UPDATE \"user\" SET num_followers = num_followers + 1 WHERE id = ?"
}
if _, err = session.Exec(rawSql, followId); err != nil {
11 years ago
session.Rollback()
return err
}
rawSql = "UPDATE user SET num_followings = num_followings + 1 WHERE id = ?"
if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" {
rawSql = "UPDATE \"user\" SET num_followings = num_followings + 1 WHERE id = ?"
}
if _, err = session.Exec(rawSql, userId); err != nil {
11 years ago
session.Rollback()
return err
}
return session.Commit()
}
11 years ago
// UnFollowUser unmarks someone be another's follower.
func UnFollowUser(userId int64, unFollowId int64) (err error) {
11 years ago
session := orm.NewSession()
defer session.Close()
session.Begin()
if _, err = session.Delete(&Follow{UserId: userId, FollowId: unFollowId}); err != nil {
11 years ago
session.Rollback()
return err
}
rawSql := "UPDATE user SET num_followers = num_followers - 1 WHERE id = ?"
if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" {
rawSql = "UPDATE \"user\" SET num_followers = num_followers - 1 WHERE id = ?"
}
if _, err = session.Exec(rawSql, unFollowId); err != nil {
11 years ago
session.Rollback()
return err
}
rawSql = "UPDATE user SET num_followings = num_followings - 1 WHERE id = ?"
if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" {
rawSql = "UPDATE \"user\" SET num_followings = num_followings - 1 WHERE id = ?"
}
if _, err = session.Exec(rawSql, userId); err != nil {
11 years ago
session.Rollback()
return err
}
return session.Commit()
}