diff options
author | Unknown <joe2010xtmf@163.com> | 2014-02-14 18:16:54 -0500 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-02-14 18:16:54 -0500 |
commit | 8ef198dface6a4e7b851ea495e236450f40ad1dc (patch) | |
tree | 6171c33b935c63aa49defb49fc4bb7a5156fd208 /models/user.go | |
parent | 237193ef2a58d890d50e96fcc99481849c42dca9 (diff) | |
download | gitea-8ef198dface6a4e7b851ea495e236450f40ad1dc.tar.gz gitea-8ef198dface6a4e7b851ea495e236450f40ad1dc.zip |
Formatting
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 79 |
1 files changed, 59 insertions, 20 deletions
diff --git a/models/user.go b/models/user.go index 4093c5ccc0..6ea329c526 100644 --- a/models/user.go +++ b/models/user.go @@ -1,3 +1,7 @@ +// 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. + package models import ( @@ -7,18 +11,21 @@ import ( "time" "github.com/dchest/scrypt" -) // user type +) + +// User types. const ( - Individual = iota + 1 - Organization + UT_INDIVIDUAL = iota + 1 + UT_ORGANIZATION ) -// login type +// Login types. const ( - Plain = iota + 1 - LDAP + LT_PLAIN = iota + 1 + LT_LDAP ) +// A User represents the object of individual and member of organization. type User struct { Id int64 LowerName string `xorm:"unique not null"` @@ -36,6 +43,7 @@ type User struct { Updated time.Time `xorm:"updated"` } +// A Follow represents type Follow struct { Id int64 UserId int64 `xorm:"unique(s)"` @@ -43,15 +51,17 @@ type Follow struct { Created time.Time `xorm:"created"` } +// Operation types of repository. const ( - OpCreateRepo = iota + 1 - OpDeleteRepo - OpStarRepo - OpFollowRepo - OpCommitRepo - OpPullRequest + OP_CREATE_REPO = iota + 1 + OP_DELETE_REPO + OP_STAR_REPO + OP_FOLLOW_REPO + OP_COMMIT_REPO + OP_PULL_REQUEST ) +// A Action represents type Action struct { Id int64 UserId int64 @@ -62,34 +72,61 @@ type Action struct { } var ( - ErrUserNotExist = errors.New("User not exist") + ErrUserAlreadyExist = errors.New("User already exist") + ErrUserNotExist = errors.New("User does not exist") ) -// user's name should be noncased unique +// IsUserExist checks if given user name exist, +// the user name should be noncased unique. func IsUserExist(name string) (bool, error) { return orm.Get(&User{LowerName: strings.ToLower(name)}) } -func RegisterUser(user *User) error { - _, err := orm.Insert(user) +// validateUser checks if user exist. +func validateUser(name string) error { + isExist, err := IsUserExist(name) + if err != nil { + return err + } else if isExist { + return ErrUserAlreadyExist + } + return nil +} + +// RegisterUser creates record of a new user. +func RegisterUser(user *User) (err error) { + if err = validateUser(user.Name); err != nil { + return err + } + _, err = orm.Insert(user) return err } -func UpdateUser(user *User) error { - _, err := orm.Id(user.Id).Update(user) +// UpdateUser updates user's information. +func UpdateUser(user *User) (err error) { + _, err = orm.Id(user.Id).Update(user) return err } +// DeleteUser completely deletes everything of the user. +func DeleteUser(user *User) error { + // TODO: check if has ownership of any repository. + _, err := orm.Delete(user) + // TODO: delete and update follower information. + return err +} + +// EncodePasswd encodes password to safe format. func (user *User) EncodePasswd(pass string) error { newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte("!#@FDEWREWR&*("), 16384, 8, 1, 64) user.Passwd = fmt.Sprintf("%x", newPasswd) return err } +// LoginUserPlain validates user by raw user name and password. func LoginUserPlain(name, passwd string) (*User, error) { user := User{Name: name} - err := user.EncodePasswd(passwd) - if err != nil { + if err := user.EncodePasswd(passwd); err != nil { return nil, err } @@ -103,6 +140,7 @@ func LoginUserPlain(name, passwd string) (*User, error) { return &user, nil } +// FollowUser marks someone be another's follower. func FollowUser(userId int64, followId int64) error { session := orm.NewSession() defer session.Close() @@ -125,6 +163,7 @@ func FollowUser(userId int64, followId int64) error { return session.Commit() } +// UnFollowUser unmarks someone be another's follower. func UnFollowUser(userId int64, unFollowId int64) error { session := orm.NewSession() defer session.Close() |