Add create organization
This commit is contained in:
parent
b2801a2e98
commit
e0f9c628c5
24 changed files with 438 additions and 197 deletions
|
@ -11,19 +11,20 @@ import (
|
|||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
// Access types.
|
||||
type AccessType int
|
||||
|
||||
const (
|
||||
AU_READABLE = iota + 1
|
||||
AU_WRITABLE
|
||||
READABLE AccessType = iota + 1
|
||||
WRITABLE
|
||||
)
|
||||
|
||||
// Access represents the accessibility of user to repository.
|
||||
type Access struct {
|
||||
Id int64
|
||||
UserName string `xorm:"unique(s)"`
|
||||
RepoName string `xorm:"unique(s)"` // <user name>/<repo name>
|
||||
Mode int `xorm:"unique(s)"`
|
||||
Created time.Time `xorm:"created"`
|
||||
UserName string `xorm:"unique(s)"`
|
||||
RepoName string `xorm:"unique(s)"` // <user name>/<repo name>
|
||||
Mode AccessType `xorm:"unique(s)"`
|
||||
Created time.Time `xorm:"created"`
|
||||
}
|
||||
|
||||
// AddAccess adds new access record.
|
||||
|
@ -59,7 +60,7 @@ func UpdateAccessWithSession(sess *xorm.Session, access *Access) error {
|
|||
|
||||
// HasAccess returns true if someone can read or write to given repository.
|
||||
// The repoName should be in format <username>/<reponame>.
|
||||
func HasAccess(uname, repoName string, mode int) (bool, error) {
|
||||
func HasAccess(uname, repoName string, mode AccessType) (bool, error) {
|
||||
if len(repoName) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
|
|
@ -213,9 +213,9 @@ func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
|
|||
// IssueUser represents an issue-user relation.
|
||||
type IssueUser struct {
|
||||
Id int64
|
||||
Uid int64 // User ID.
|
||||
Uid int64 `xorm:"INDEX"` // User ID.
|
||||
IssueId int64
|
||||
RepoId int64
|
||||
RepoId int64 `xorm:"INDEX"`
|
||||
MilestoneId int64
|
||||
IsRead bool
|
||||
IsAssigned bool
|
||||
|
|
|
@ -255,7 +255,7 @@ func LoginUserLdapSource(user *User, name, passwd string, sourceId int64, cfg *L
|
|||
Email: mail,
|
||||
}
|
||||
|
||||
return RegisterUser(user)
|
||||
return CreateUser(user)
|
||||
}
|
||||
|
||||
type loginAuth struct {
|
||||
|
@ -359,5 +359,5 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S
|
|||
Passwd: passwd,
|
||||
Email: name,
|
||||
}
|
||||
return RegisterUser(user)
|
||||
return CreateUser(user)
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ func init() {
|
|||
tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch),
|
||||
new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow),
|
||||
new(Mirror), new(Release), new(LoginSource), new(Webhook), new(IssueUser),
|
||||
new(Milestone), new(Label), new(HookTask))
|
||||
new(Milestone), new(Label), new(HookTask), new(Team), new(OrgUser), new(TeamUser))
|
||||
}
|
||||
|
||||
func LoadModelsConfig() {
|
||||
|
|
69
models/org.go
Normal file
69
models/org.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
// 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 AuthorizeType int
|
||||
|
||||
const (
|
||||
ORG_READABLE AuthorizeType = iota + 1
|
||||
ORG_WRITABLE
|
||||
ORG_ADMIN
|
||||
)
|
||||
|
||||
// Team represents a organization team.
|
||||
type Team struct {
|
||||
Id int64
|
||||
OrgId int64 `xorm:"INDEX"`
|
||||
Name string
|
||||
Description string
|
||||
Authorize AuthorizeType
|
||||
NumMembers int
|
||||
NumRepos int
|
||||
}
|
||||
|
||||
// NewTeam creates a record of new team.
|
||||
func NewTeam(t *Team) error {
|
||||
_, err := x.Insert(t)
|
||||
return err
|
||||
}
|
||||
|
||||
// ________ ____ ___
|
||||
// \_____ \_______ ____ | | \______ ___________
|
||||
// / | \_ __ \/ ___\| | / ___// __ \_ __ \
|
||||
// / | \ | \/ /_/ > | /\___ \\ ___/| | \/
|
||||
// \_______ /__| \___ /|______//____ >\___ >__|
|
||||
// \/ /_____/ \/ \/
|
||||
|
||||
// OrgUser represents an organization-user relation.
|
||||
type OrgUser struct {
|
||||
Id int64
|
||||
Uid int64 `xorm:"INDEX"`
|
||||
OrgId int64 `xorm:"INDEX"`
|
||||
IsPublic bool
|
||||
IsOwner bool
|
||||
NumTeam int
|
||||
}
|
||||
|
||||
// GetOrgUsersByUserId returns all organization-user relations by user ID.
|
||||
func GetOrgUsersByUserId(uid int64) ([]*OrgUser, error) {
|
||||
ous := make([]*OrgUser, 0, 10)
|
||||
err := x.Where("uid=?", uid).Find(&ous)
|
||||
return ous, err
|
||||
}
|
||||
|
||||
// ___________ ____ ___
|
||||
// \__ ___/___ _____ _____ | | \______ ___________
|
||||
// | |_/ __ \\__ \ / \| | / ___// __ \_ __ \
|
||||
// | |\ ___/ / __ \| Y Y \ | /\___ \\ ___/| | \/
|
||||
// |____| \___ >____ /__|_| /______//____ >\___ >__|
|
||||
// \/ \/ \/ \/ \/
|
||||
|
||||
// TeamUser represents an team-user relation.
|
||||
type TeamUser struct {
|
||||
Id int64
|
||||
Uid int64
|
||||
OrgId int64 `xorm:"INDEX"`
|
||||
TeamId int64
|
||||
}
|
|
@ -158,7 +158,7 @@ func IsRepositoryExist(u *User, repoName string) (bool, error) {
|
|||
}
|
||||
|
||||
var (
|
||||
illegalEquals = []string{"raw", "install", "api", "avatar", "user", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin"}
|
||||
illegalEquals = []string{"raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin"}
|
||||
illegalSuffixs = []string{".git"}
|
||||
)
|
||||
|
||||
|
@ -483,7 +483,9 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir
|
|||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
sess.Begin()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err = sess.Insert(repo); err != nil {
|
||||
if err2 := os.RemoveAll(repoPath); err2 != nil {
|
||||
|
@ -495,9 +497,9 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir
|
|||
return nil, err
|
||||
}
|
||||
|
||||
mode := AU_WRITABLE
|
||||
mode := WRITABLE
|
||||
if mirror {
|
||||
mode = AU_READABLE
|
||||
mode = READABLE
|
||||
}
|
||||
access := Access{
|
||||
UserName: user.LowerName,
|
||||
|
|
190
models/user.go
190
models/user.go
|
@ -21,10 +21,11 @@ import (
|
|||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
// User types.
|
||||
type UserType int
|
||||
|
||||
const (
|
||||
UT_INDIVIDUAL = iota + 1
|
||||
UT_ORGANIZATION
|
||||
INDIVIDUAL UserType = iota // Historic reason to make it starts at 0.
|
||||
ORGANIZATION
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -50,7 +51,8 @@ type User struct {
|
|||
LoginType LoginType
|
||||
LoginSource int64 `xorm:"not null default 0"`
|
||||
LoginName string
|
||||
Type int
|
||||
Type UserType
|
||||
Orgs []*User `xorm:"-"`
|
||||
NumFollowers int
|
||||
NumFollowings int
|
||||
NumStars int
|
||||
|
@ -65,36 +67,60 @@ type User struct {
|
|||
Salt string `xorm:"VARCHAR(10)"`
|
||||
Created time.Time `xorm:"created"`
|
||||
Updated time.Time `xorm:"updated"`
|
||||
|
||||
// For organization.
|
||||
NumTeams int
|
||||
NumMembers int
|
||||
}
|
||||
|
||||
// HomeLink returns the user home page link.
|
||||
func (user *User) HomeLink() string {
|
||||
return "/user/" + user.Name
|
||||
func (u *User) HomeLink() string {
|
||||
return "/user/" + u.Name
|
||||
}
|
||||
|
||||
// AvatarLink returns user gravatar link.
|
||||
func (user *User) AvatarLink() string {
|
||||
func (u *User) AvatarLink() string {
|
||||
if setting.DisableGravatar {
|
||||
return "/img/avatar_default.jpg"
|
||||
} else if setting.Service.EnableCacheAvatar {
|
||||
return "/avatar/" + user.Avatar
|
||||
return "/avatar/" + u.Avatar
|
||||
}
|
||||
return "//1.gravatar.com/avatar/" + user.Avatar
|
||||
return "//1.gravatar.com/avatar/" + u.Avatar
|
||||
}
|
||||
|
||||
// NewGitSig generates and returns the signature of given user.
|
||||
func (user *User) NewGitSig() *git.Signature {
|
||||
func (u *User) NewGitSig() *git.Signature {
|
||||
return &git.Signature{
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
Name: u.Name,
|
||||
Email: u.Email,
|
||||
When: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// EncodePasswd encodes password to safe format.
|
||||
func (user *User) EncodePasswd() {
|
||||
newPasswd := base.PBKDF2([]byte(user.Passwd), []byte(user.Salt), 10000, 50, sha256.New)
|
||||
user.Passwd = fmt.Sprintf("%x", newPasswd)
|
||||
func (u *User) EncodePasswd() {
|
||||
newPasswd := base.PBKDF2([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New)
|
||||
u.Passwd = fmt.Sprintf("%x", newPasswd)
|
||||
}
|
||||
|
||||
func (u *User) IsOrganization() bool {
|
||||
return u.Type == ORGANIZATION
|
||||
}
|
||||
|
||||
func (u *User) GetOrganizations() error {
|
||||
ous, err := GetOrgUsersByUserId(u.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
u.Orgs = make([]*User, len(ous))
|
||||
for i, ou := range ous {
|
||||
u.Orgs[i], err = GetUserById(ou.OrgId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Member represents user is member of organization.
|
||||
|
@ -126,49 +152,135 @@ func GetUserSalt() string {
|
|||
return base.GetRandomString(10)
|
||||
}
|
||||
|
||||
// RegisterUser creates record of a new user.
|
||||
func RegisterUser(user *User) (*User, error) {
|
||||
|
||||
if !IsLegalName(user.Name) {
|
||||
// CreateUser creates record of a new user.
|
||||
func CreateUser(u *User) (*User, error) {
|
||||
if !IsLegalName(u.Name) {
|
||||
return nil, ErrUserNameIllegal
|
||||
}
|
||||
|
||||
isExist, err := IsUserExist(user.Name)
|
||||
isExist, err := IsUserExist(u.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if isExist {
|
||||
return nil, ErrUserAlreadyExist
|
||||
}
|
||||
|
||||
isExist, err = IsEmailUsed(user.Email)
|
||||
isExist, err = IsEmailUsed(u.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.Rands = GetUserSalt()
|
||||
user.Salt = GetUserSalt()
|
||||
user.EncodePasswd()
|
||||
if _, err = x.Insert(user); err != nil {
|
||||
return nil, err
|
||||
} else if err = os.MkdirAll(UserPath(user.Name), os.ModePerm); err != nil {
|
||||
if _, err := x.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))
|
||||
}
|
||||
u.LowerName = strings.ToLower(u.Name)
|
||||
u.Avatar = base.EncodeMd5(u.Email)
|
||||
u.AvatarEmail = u.Email
|
||||
u.Rands = GetUserSalt()
|
||||
u.Salt = GetUserSalt()
|
||||
u.EncodePasswd()
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if user.Id == 1 {
|
||||
user.IsAdmin = true
|
||||
user.IsActive = true
|
||||
_, err = x.Id(user.Id).UseBool().Update(user)
|
||||
if _, err = sess.Insert(u); err != nil {
|
||||
sess.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
return user, err
|
||||
|
||||
if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {
|
||||
sess.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Auto-set admin for user whose ID is 1.
|
||||
if u.Id == 1 {
|
||||
u.IsAdmin = true
|
||||
u.IsActive = true
|
||||
_, err = x.Id(u.Id).UseBool().Update(u)
|
||||
}
|
||||
return u, err
|
||||
}
|
||||
|
||||
// CreateOrganization creates record of a new organization.
|
||||
func CreateOrganization(org, owner *User) (*User, error) {
|
||||
if !IsLegalName(org.Name) {
|
||||
return nil, ErrUserNameIllegal
|
||||
}
|
||||
|
||||
isExist, err := IsUserExist(org.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if isExist {
|
||||
return nil, ErrUserAlreadyExist
|
||||
}
|
||||
|
||||
isExist, err = IsEmailUsed(org.Email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if isExist {
|
||||
return nil, ErrEmailAlreadyUsed
|
||||
}
|
||||
|
||||
org.LowerName = strings.ToLower(org.Name)
|
||||
org.Avatar = base.EncodeMd5(org.Email)
|
||||
org.AvatarEmail = org.Email
|
||||
// No password for organization.
|
||||
org.NumTeams = 1
|
||||
org.NumMembers = 1
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err = sess.Insert(org); err != nil {
|
||||
sess.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create default owner team.
|
||||
t := &Team{
|
||||
OrgId: org.Id,
|
||||
Name: "Owner",
|
||||
Authorize: ORG_ADMIN,
|
||||
NumMembers: 1,
|
||||
}
|
||||
if _, err = sess.Insert(t); err != nil {
|
||||
sess.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add initial creator to organization and owner team.
|
||||
ou := &OrgUser{
|
||||
Uid: owner.Id,
|
||||
OrgId: org.Id,
|
||||
IsOwner: true,
|
||||
NumTeam: 1,
|
||||
}
|
||||
if _, err = sess.Insert(ou); err != nil {
|
||||
sess.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tu := &TeamUser{
|
||||
Uid: owner.Id,
|
||||
OrgId: org.Id,
|
||||
TeamId: t.Id,
|
||||
}
|
||||
if _, err = sess.Insert(tu); err != nil {
|
||||
sess.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return org, sess.Commit()
|
||||
}
|
||||
|
||||
// GetUsers returns given number of user objects with offset.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue