merged
This commit is contained in:
commit
17da2fd2e3
58 changed files with 1831 additions and 344 deletions
|
@ -64,6 +64,10 @@ func CommitRepoAction(userId int64, userName string,
|
|||
watches = append(watches, Watch{UserId: userId})
|
||||
|
||||
for i := range watches {
|
||||
if userId == watches[i].UserId && i > 0 {
|
||||
continue // Do not add twice in case author watches his/her repository.
|
||||
}
|
||||
|
||||
_, err = orm.InsertOne(&Action{
|
||||
UserId: watches[i].UserId,
|
||||
ActUserId: userId,
|
||||
|
|
19
models/issue.go
Normal file
19
models/issue.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
// 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
|
||||
|
||||
type Issue struct {
|
||||
Id int64
|
||||
RepoId int64 `xorm:"index"`
|
||||
PosterId int64
|
||||
}
|
||||
|
||||
type PullRequest struct {
|
||||
Id int64
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
Id int64
|
||||
}
|
|
@ -7,6 +7,7 @@ package models
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
|
@ -16,48 +17,37 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
orm *xorm.Engine
|
||||
RepoRootPath string
|
||||
orm *xorm.Engine
|
||||
|
||||
DbCfg struct {
|
||||
Type, Host, Name, User, Pwd, Path, SslMode string
|
||||
}
|
||||
)
|
||||
|
||||
type Members struct {
|
||||
Id int64
|
||||
OrgId int64 `xorm:"unique(s) index"`
|
||||
UserId int64 `xorm:"unique(s)"`
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
Id int64
|
||||
RepoId int64 `xorm:"index"`
|
||||
PosterId int64
|
||||
}
|
||||
|
||||
type PullRequest struct {
|
||||
Id int64
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
Id int64
|
||||
func LoadModelsConfig() {
|
||||
DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE")
|
||||
DbCfg.Host = base.Cfg.MustValue("database", "HOST")
|
||||
DbCfg.Name = base.Cfg.MustValue("database", "NAME")
|
||||
DbCfg.User = base.Cfg.MustValue("database", "USER")
|
||||
DbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD")
|
||||
DbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE")
|
||||
DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db")
|
||||
}
|
||||
|
||||
func setEngine() {
|
||||
dbType := base.Cfg.MustValue("database", "DB_TYPE")
|
||||
dbHost := base.Cfg.MustValue("database", "HOST")
|
||||
dbName := base.Cfg.MustValue("database", "NAME")
|
||||
dbUser := base.Cfg.MustValue("database", "USER")
|
||||
dbPwd := base.Cfg.MustValue("database", "PASSWD")
|
||||
sslMode := base.Cfg.MustValue("database", "SSL_MODE")
|
||||
|
||||
var err error
|
||||
switch dbType {
|
||||
switch DbCfg.Type {
|
||||
case "mysql":
|
||||
orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@%s/%s?charset=utf8",
|
||||
dbUser, dbPwd, dbHost, dbName))
|
||||
DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
|
||||
case "postgres":
|
||||
orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
|
||||
dbUser, dbPwd, dbName, sslMode))
|
||||
DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
|
||||
case "sqlite3":
|
||||
os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
|
||||
orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
|
||||
default:
|
||||
fmt.Printf("Unknown database type: %s\n", dbType)
|
||||
fmt.Printf("Unknown database type: %s\n", DbCfg.Type)
|
||||
os.Exit(2)
|
||||
}
|
||||
if err != nil {
|
||||
|
@ -65,8 +55,8 @@ func setEngine() {
|
|||
os.Exit(2)
|
||||
}
|
||||
|
||||
// TODO: for serv command, MUST remove the output to os.stdout, so
|
||||
// use log file to instead print to stdout
|
||||
// WARNNING: for serv command, MUST remove the output to os.stdout,
|
||||
// so use log file to instead print to stdout.
|
||||
|
||||
//x.ShowDebug = true
|
||||
//orm.ShowErr = true
|
||||
|
@ -77,20 +67,29 @@ func setEngine() {
|
|||
}
|
||||
orm.Logger = f
|
||||
orm.ShowSQL = true
|
||||
|
||||
// Determine and create root git reposiroty path.
|
||||
RepoRootPath = base.Cfg.MustValue("repository", "ROOT")
|
||||
if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
|
||||
fmt.Printf("models.init(fail to create RepoRootPath(%s)): %v\n", RepoRootPath, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
func NewEngine() {
|
||||
setEngine()
|
||||
if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Access),
|
||||
new(Action), new(Watch)); err != nil {
|
||||
if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
|
||||
new(Action), new(Access)); err != nil {
|
||||
fmt.Printf("sync database struct error: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
type Statistic struct {
|
||||
Counter struct {
|
||||
User, PublicKey, Repo, Watch, Action, Access int64
|
||||
}
|
||||
}
|
||||
|
||||
func GetStatistic() (stats Statistic) {
|
||||
stats.Counter.User, _ = orm.Count(new(User))
|
||||
stats.Counter.PublicKey, _ = orm.Count(new(PublicKey))
|
||||
stats.Counter.Repo, _ = orm.Count(new(Repository))
|
||||
stats.Counter.Watch, _ = orm.Count(new(Watch))
|
||||
stats.Counter.Action, _ = orm.Count(new(Action))
|
||||
stats.Counter.Access, _ = orm.Count(new(Access))
|
||||
return stats
|
||||
}
|
||||
|
|
|
@ -13,6 +13,9 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
LoadModelsConfig()
|
||||
NewEngine()
|
||||
|
||||
var err error
|
||||
orm, err = xorm.NewEngine("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
|
|
|
@ -27,8 +27,12 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
sshOpLocker = sync.Mutex{}
|
||||
ErrKeyAlreadyExist = errors.New("Public key already exist")
|
||||
)
|
||||
|
||||
var sshOpLocker = sync.Mutex{}
|
||||
|
||||
var (
|
||||
sshPath string
|
||||
appPath string
|
||||
)
|
||||
|
@ -79,10 +83,6 @@ type PublicKey struct {
|
|||
Updated time.Time `xorm:"updated"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrKeyAlreadyExist = errors.New("Public key already exist")
|
||||
)
|
||||
|
||||
// GenAuthorizedKey returns formatted public key string.
|
||||
func GenAuthorizedKey(keyId int64, key string) string {
|
||||
return fmt.Sprintf(TPL_PUBLICK_KEY+"\n", appPath, keyId, key)
|
||||
|
|
276
models/repo.go
276
models/repo.go
|
@ -12,6 +12,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -26,68 +27,26 @@ import (
|
|||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
// Repository represents a git repository.
|
||||
type Repository struct {
|
||||
Id int64
|
||||
OwnerId int64 `xorm:"unique(s)"`
|
||||
ForkId int64
|
||||
LowerName string `xorm:"unique(s) index not null"`
|
||||
Name string `xorm:"index not null"`
|
||||
Description string
|
||||
Website string
|
||||
Private bool
|
||||
NumWatchs int
|
||||
NumStars int
|
||||
NumForks int
|
||||
Created time.Time `xorm:"created"`
|
||||
Updated time.Time `xorm:"updated"`
|
||||
}
|
||||
var (
|
||||
ErrRepoAlreadyExist = errors.New("Repository already exist")
|
||||
ErrRepoNotExist = errors.New("Repository does not exist")
|
||||
ErrRepoFileNotExist = errors.New("Target Repo file does not exist")
|
||||
ErrRepoNameIllegal = errors.New("Repository name contains illegal characters")
|
||||
ErrRepoFileNotLoaded = fmt.Errorf("repo file not loaded")
|
||||
)
|
||||
|
||||
// Watch is connection request for receiving repository notifycation.
|
||||
type Watch struct {
|
||||
Id int64
|
||||
RepoId int64 `xorm:"UNIQUE(watch)"`
|
||||
UserId int64 `xorm:"UNIQUE(watch)"`
|
||||
}
|
||||
|
||||
// Watch or unwatch repository.
|
||||
func WatchRepo(userId, repoId int64, watch bool) (err error) {
|
||||
if watch {
|
||||
_, err = orm.Insert(&Watch{RepoId: repoId, UserId: userId})
|
||||
} else {
|
||||
_, err = orm.Delete(&Watch{0, repoId, userId})
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// GetWatches returns all watches of given repository.
|
||||
func GetWatches(repoId int64) ([]Watch, error) {
|
||||
watches := make([]Watch, 0, 10)
|
||||
err := orm.Find(&watches, &Watch{RepoId: repoId})
|
||||
return watches, err
|
||||
}
|
||||
|
||||
// IsWatching checks if user has watched given repository.
|
||||
func IsWatching(userId, repoId int64) bool {
|
||||
has, _ := orm.Get(&Watch{0, repoId, userId})
|
||||
return has
|
||||
}
|
||||
var gitInitLocker = sync.Mutex{}
|
||||
|
||||
var (
|
||||
gitInitLocker = sync.Mutex{}
|
||||
LanguageIgns, Licenses []string
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRepoAlreadyExist = errors.New("Repository already exist")
|
||||
ErrRepoNotExist = errors.New("Repository does not exist")
|
||||
ErrRepoFileNotExist = errors.New("Target Repo file does not exist")
|
||||
)
|
||||
|
||||
func init() {
|
||||
func LoadRepoConfig() {
|
||||
LanguageIgns = strings.Split(base.Cfg.MustValue("repository", "LANG_IGNS"), "|")
|
||||
Licenses = strings.Split(base.Cfg.MustValue("repository", "LICENSES"), "|")
|
||||
}
|
||||
|
||||
func NewRepoContext() {
|
||||
zip.Verbose = false
|
||||
|
||||
// Check if server has basic git setting.
|
||||
|
@ -104,6 +63,32 @@ func init() {
|
|||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize illegal patterns.
|
||||
for i := range illegalPatterns[1:] {
|
||||
pattern := ""
|
||||
for j := range illegalPatterns[i+1] {
|
||||
pattern += "[" + string(illegalPatterns[i+1][j]-32) + string(illegalPatterns[i+1][j]) + "]"
|
||||
}
|
||||
illegalPatterns[i+1] = pattern
|
||||
}
|
||||
}
|
||||
|
||||
// Repository represents a git repository.
|
||||
type Repository struct {
|
||||
Id int64
|
||||
OwnerId int64 `xorm:"unique(s)"`
|
||||
ForkId int64
|
||||
LowerName string `xorm:"unique(s) index not null"`
|
||||
Name string `xorm:"index not null"`
|
||||
Description string
|
||||
Website string
|
||||
Private bool
|
||||
NumWatches int
|
||||
NumStars int
|
||||
NumForks int
|
||||
Created time.Time `xorm:"created"`
|
||||
Updated time.Time `xorm:"updated"`
|
||||
}
|
||||
|
||||
// IsRepositoryExist returns true if the repository with given name under user has already existed.
|
||||
|
@ -120,8 +105,28 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) {
|
|||
return s.IsDir(), nil
|
||||
}
|
||||
|
||||
var (
|
||||
// Define as all lower case!!
|
||||
illegalPatterns = []string{"[.][Gg][Ii][Tt]", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template", "admin"}
|
||||
)
|
||||
|
||||
// IsLegalName returns false if name contains illegal characters.
|
||||
func IsLegalName(repoName string) bool {
|
||||
for _, pattern := range illegalPatterns {
|
||||
has, _ := regexp.MatchString(pattern, repoName)
|
||||
if has {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// CreateRepository creates a repository for given user or orgnaziation.
|
||||
func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) {
|
||||
if !IsLegalName(repoName) {
|
||||
return nil, ErrRepoNameIllegal
|
||||
}
|
||||
|
||||
isExist, err := IsRepositoryExist(user, repoName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -331,6 +336,82 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep
|
|||
return nil
|
||||
}
|
||||
|
||||
// UserRepo reporesents a repository with user name.
|
||||
type UserRepo struct {
|
||||
*Repository
|
||||
UserName string
|
||||
}
|
||||
|
||||
// GetRepos returns given number of repository objects with offset.
|
||||
func GetRepos(num, offset int) ([]UserRepo, error) {
|
||||
repos := make([]Repository, 0, num)
|
||||
if err := orm.Limit(num, offset).Asc("id").Find(&repos); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
urepos := make([]UserRepo, len(repos))
|
||||
for i := range repos {
|
||||
urepos[i].Repository = &repos[i]
|
||||
u := new(User)
|
||||
has, err := orm.Id(urepos[i].Repository.OwnerId).Get(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrUserNotExist
|
||||
}
|
||||
urepos[i].UserName = u.Name
|
||||
}
|
||||
|
||||
return urepos, nil
|
||||
}
|
||||
|
||||
func RepoPath(userName, repoName string) string {
|
||||
return filepath.Join(UserPath(userName), repoName+".git")
|
||||
}
|
||||
|
||||
// DeleteRepository deletes a repository for a user or orgnaztion.
|
||||
func DeleteRepository(userId, repoId int64, userName string) (err error) {
|
||||
repo := &Repository{Id: repoId, OwnerId: userId}
|
||||
has, err := orm.Get(repo)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return ErrRepoNotExist
|
||||
}
|
||||
|
||||
session := orm.NewSession()
|
||||
if err = session.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = session.Delete(&Repository{Id: repoId}); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := session.Delete(&Access{UserName: userName, RepoName: repo.Name}); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
rawSql := "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?"
|
||||
if _, err = session.Exec(rawSql, userId); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err = session.Delete(&Watch{RepoId: repoId}); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err = session.Commit(); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
|
||||
// TODO: log and delete manully
|
||||
log.Error("delete repo %s/%s failed: %v", userName, repo.Name, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRepositoryByName returns the repository by given name under user if exists.
|
||||
func GetRepositoryByName(user *User, repoName string) (*Repository, error) {
|
||||
repo := &Repository{
|
||||
|
@ -368,6 +449,45 @@ func GetRepositoryCount(user *User) (int64, error) {
|
|||
return orm.Count(&Repository{OwnerId: user.Id})
|
||||
}
|
||||
|
||||
// Watch is connection request for receiving repository notifycation.
|
||||
type Watch struct {
|
||||
Id int64
|
||||
RepoId int64 `xorm:"UNIQUE(watch)"`
|
||||
UserId int64 `xorm:"UNIQUE(watch)"`
|
||||
}
|
||||
|
||||
// Watch or unwatch repository.
|
||||
func WatchRepo(userId, repoId int64, watch bool) (err error) {
|
||||
if watch {
|
||||
if _, err = orm.Insert(&Watch{RepoId: repoId, UserId: userId}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rawSql := "UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?"
|
||||
_, err = orm.Exec(rawSql, repoId)
|
||||
} else {
|
||||
if _, err = orm.Delete(&Watch{0, repoId, userId}); err != nil {
|
||||
return err
|
||||
}
|
||||
rawSql := "UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?"
|
||||
_, err = orm.Exec(rawSql, repoId)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// GetWatches returns all watches of given repository.
|
||||
func GetWatches(repoId int64) ([]Watch, error) {
|
||||
watches := make([]Watch, 0, 10)
|
||||
err := orm.Find(&watches, &Watch{RepoId: repoId})
|
||||
return watches, err
|
||||
}
|
||||
|
||||
// IsWatching checks if user has watched given repository.
|
||||
func IsWatching(userId, repoId int64) bool {
|
||||
has, _ := orm.Get(&Watch{0, repoId, userId})
|
||||
return has
|
||||
}
|
||||
|
||||
func StarReposiory(user *User, repoName string) error {
|
||||
return nil
|
||||
}
|
||||
|
@ -388,56 +508,6 @@ func ForkRepository(reposName string, userId int64) {
|
|||
|
||||
}
|
||||
|
||||
func RepoPath(userName, repoName string) string {
|
||||
return filepath.Join(UserPath(userName), repoName+".git")
|
||||
}
|
||||
|
||||
// DeleteRepository deletes a repository for a user or orgnaztion.
|
||||
func DeleteRepository(userId, repoId int64, userName string) (err error) {
|
||||
repo := &Repository{Id: repoId, OwnerId: userId}
|
||||
has, err := orm.Get(repo)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return ErrRepoNotExist
|
||||
}
|
||||
|
||||
session := orm.NewSession()
|
||||
if err = session.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = session.Delete(&Repository{Id: repoId}); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := session.Delete(&Access{UserName: userName, RepoName: repo.Name}); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
rawSql := "UPDATE user SET num_repos = num_repos - 1 WHERE id = ?"
|
||||
if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" {
|
||||
rawSql = "UPDATE \"user\" SET num_repos = num_repos - 1 WHERE id = ?"
|
||||
}
|
||||
if _, err = session.Exec(rawSql, userId); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err = session.Commit(); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
|
||||
// TODO: log and delete manully
|
||||
log.Error("delete repo %s/%s failed: %v", userName, repo.Name, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
ErrRepoFileNotLoaded = fmt.Errorf("repo file not loaded")
|
||||
)
|
||||
|
||||
// RepoFile represents a file object in git repository.
|
||||
type RepoFile struct {
|
||||
*git.TreeEntry
|
||||
|
|
105
models/user.go
105
models/user.go
|
@ -33,6 +33,14 @@ const (
|
|||
LT_LDAP
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUserOwnRepos = errors.New("User still have ownership of repositories")
|
||||
ErrUserAlreadyExist = errors.New("User already exist")
|
||||
ErrUserNotExist = errors.New("User does not exist")
|
||||
ErrEmailAlreadyUsed = errors.New("E-mail already used")
|
||||
ErrUserNameIllegal = errors.New("User name contains illegal characters")
|
||||
)
|
||||
|
||||
// User represents the object of individual and member of organization.
|
||||
type User struct {
|
||||
Id int64
|
||||
|
@ -51,6 +59,7 @@ type User struct {
|
|||
Location string
|
||||
Website string
|
||||
IsActive bool
|
||||
IsAdmin bool
|
||||
Rands string `xorm:"VARCHAR(10)"`
|
||||
Created time.Time `xorm:"created"`
|
||||
Updated time.Time `xorm:"updated"`
|
||||
|
@ -66,19 +75,28 @@ func (user *User) AvatarLink() string {
|
|||
return "http://1.gravatar.com/avatar/" + user.Avatar
|
||||
}
|
||||
|
||||
type Follow struct {
|
||||
Id int64
|
||||
UserId int64 `xorm:"unique(s)"`
|
||||
FollowId int64 `xorm:"unique(s)"`
|
||||
Created time.Time `xorm:"created"`
|
||||
// NewGitSig generates and returns the signature of given user.
|
||||
func (user *User) NewGitSig() *git.Signature {
|
||||
return &git.Signature{
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
When: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
ErrUserOwnRepos = errors.New("User still have ownership of repositories")
|
||||
ErrUserAlreadyExist = errors.New("User already exist")
|
||||
ErrUserNotExist = errors.New("User does not exist")
|
||||
ErrEmailAlreadyUsed = errors.New("E-mail already used")
|
||||
)
|
||||
// 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)
|
||||
user.Passwd = fmt.Sprintf("%x", newPasswd)
|
||||
return err
|
||||
}
|
||||
|
||||
// Member represents user is member of organization.
|
||||
type Member struct {
|
||||
Id int64
|
||||
OrgId int64 `xorm:"unique(member) index"`
|
||||
UserId int64 `xorm:"unique(member)"`
|
||||
}
|
||||
|
||||
// IsUserExist checks if given user name exist,
|
||||
// the user name should be noncased unique.
|
||||
|
@ -91,15 +109,6 @@ func IsEmailUsed(email string) (bool, error) {
|
|||
return orm.Get(&User{Email: email})
|
||||
}
|
||||
|
||||
// NewGitSig generates and returns the signature of given user.
|
||||
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)
|
||||
|
@ -107,6 +116,10 @@ func GetUserSalt() string {
|
|||
|
||||
// RegisterUser creates record of a new user.
|
||||
func RegisterUser(user *User) (*User, error) {
|
||||
if !IsLegalName(user.Name) {
|
||||
return nil, ErrUserNameIllegal
|
||||
}
|
||||
|
||||
isExist, err := IsUserExist(user.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -136,7 +149,20 @@ func RegisterUser(user *User) (*User, error) {
|
|||
}
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
|
||||
if user.Id == 1 {
|
||||
user.IsAdmin = true
|
||||
user.IsActive = true
|
||||
_, err = orm.Id(user.Id).UseBool().Update(user)
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
// GetUsers returns given number of user objects with offset.
|
||||
func GetUsers(num, offset int) ([]User, error) {
|
||||
users := make([]User, 0, num)
|
||||
err := orm.Limit(num, offset).Asc("id").Find(&users)
|
||||
return users, err
|
||||
}
|
||||
|
||||
// get user by erify code
|
||||
|
@ -217,22 +243,14 @@ func DeleteUser(user *User) error {
|
|||
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)
|
||||
user.Passwd = fmt.Sprintf("%x", newPasswd)
|
||||
return err
|
||||
}
|
||||
|
||||
// UserPath returns the path absolute path of user repositories.
|
||||
func UserPath(userName string) string {
|
||||
return filepath.Join(RepoRootPath, strings.ToLower(userName))
|
||||
return filepath.Join(base.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=?"
|
||||
|
||||
has, err := orm.Sql(rawSql, keyId).Get(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -289,6 +307,13 @@ func LoginUserPlain(name, passwd string) (*User, error) {
|
|||
return &user, err
|
||||
}
|
||||
|
||||
// Follow is connection request for receiving user notifycation.
|
||||
type Follow struct {
|
||||
Id int64
|
||||
UserId int64 `xorm:"unique(follow)"`
|
||||
FollowId int64 `xorm:"unique(follow)"`
|
||||
}
|
||||
|
||||
// FollowUser marks someone be another's follower.
|
||||
func FollowUser(userId int64, followId int64) (err error) {
|
||||
session := orm.NewSession()
|
||||
|
@ -300,19 +325,13 @@ func FollowUser(userId int64, followId int64) (err error) {
|
|||
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 = ?"
|
||||
}
|
||||
rawSql := "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?"
|
||||
if _, err = session.Exec(rawSql, followId); err != nil {
|
||||
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 = ?"
|
||||
}
|
||||
rawSql = "UPDATE `user` SET num_followings = num_followings + 1 WHERE id = ?"
|
||||
if _, err = session.Exec(rawSql, userId); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
|
@ -331,19 +350,13 @@ func UnFollowUser(userId int64, unFollowId int64) (err error) {
|
|||
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 = ?"
|
||||
}
|
||||
rawSql := "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?"
|
||||
if _, err = session.Exec(rawSql, unFollowId); err != nil {
|
||||
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 = ?"
|
||||
}
|
||||
rawSql = "UPDATE `user` SET num_followings = num_followings - 1 WHERE id = ?"
|
||||
if _, err = session.Exec(rawSql, userId); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue