|
@@ -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()
|