diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-08-15 18:29:41 +0800 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-08-15 18:29:41 +0800 |
commit | 36b4c57ff13198196134b853f624fb633061734d (patch) | |
tree | e973d2aad41823e50c951a08ec8742030141d675 /models | |
parent | 3e32b14ad4da2db1b05eeec73936dbbe79cecc45 (diff) | |
download | gitea-36b4c57ff13198196134b853f624fb633061734d.tar.gz gitea-36b4c57ff13198196134b853f624fb633061734d.zip |
Finish new organization members and invitation page
Diffstat (limited to 'models')
-rw-r--r-- | models/org.go | 94 | ||||
-rw-r--r-- | models/user.go | 10 |
2 files changed, 100 insertions, 4 deletions
diff --git a/models/org.go b/models/org.go index edae828b54..b40b313bc3 100644 --- a/models/org.go +++ b/models/org.go @@ -59,6 +59,16 @@ func (org *User) GetMembers() error { return nil } +// AddMember adds new member to organization. +func (org *User) AddMember(uid int64) error { + return AddOrgUser(org.Id, uid) +} + +// RemoveMember removes member from organization. +func (org *User) RemoveMember(uid int64) error { + return RemoveOrgUser(org.Id, uid) +} + // CreateOrganization creates record of a new organization. func CreateOrganization(org, owner *User) (*User, error) { if !IsLegalName(org.Name) { @@ -241,8 +251,7 @@ func NewTeam(t *Team) error { } // Update organization number of teams. - rawSql := "UPDATE `user` SET num_teams = num_teams + 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, t.OrgId); err != nil { + if _, err = sess.Exec("UPDATE `user` SET num_teams = num_teams + 1 WHERE id = ?", t.OrgId); err != nil { sess.Rollback() return err } @@ -270,8 +279,8 @@ func UpdateTeam(t *Team) error { // OrgUser represents an organization-user relation. type OrgUser struct { Id int64 - Uid int64 `xorm:"INDEX"` - OrgId int64 `xorm:"INDEX"` + Uid int64 `xorm:"INDEX UNIQUE(s)"` + OrgId int64 `xorm:"INDEX UNIQUE(s)"` IsPublic bool IsOwner bool NumTeam int @@ -289,6 +298,12 @@ func IsOrganizationMember(orgId, uid int64) bool { return has } +// IsPublicMembership returns ture if given user public his/her membership. +func IsPublicMembership(orgId, uid int64) bool { + has, _ := x.Where("uid=?", uid).And("org_id=?", orgId).And("is_public=?", true).Get(new(OrgUser)) + return has +} + // GetOrgUsersByUserId returns all organization-user relations by user ID. func GetOrgUsersByUserId(uid int64) ([]*OrgUser, error) { ous := make([]*OrgUser, 0, 10) @@ -303,6 +318,77 @@ func GetOrgUsersByOrgId(orgId int64) ([]*OrgUser, error) { return ous, err } +// ChangeOrgUserStatus changes public or private membership status. +func ChangeOrgUserStatus(orgId, uid int64, public bool) error { + ou := new(OrgUser) + has, err := x.Where("uid=?", uid).And("org_id=?", orgId).Get(ou) + if err != nil { + return err + } else if !has { + return nil + } + + ou.IsPublic = public + _, err = x.Id(ou.Id).AllCols().Update(ou) + return err +} + +// AddOrgUser adds new user to given organization. +func AddOrgUser(orgId, uid int64) error { + if IsOrganizationMember(orgId, uid) { + return nil + } + + ou := &OrgUser{ + Uid: uid, + OrgId: orgId, + } + + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + if _, err := sess.Insert(ou); err != nil { + sess.Rollback() + return err + } else if _, err = sess.Exec("UPDATE `user` SET num_members = num_members + 1 WHERE id = ?", orgId); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() +} + +// RemoveOrgUser removes user from given organization. +func RemoveOrgUser(orgId, uid int64) error { + ou := new(OrgUser) + + has, err := x.Where("uid=?", uid).And("org_id=?", orgId).Get(ou) + if err != nil { + return err + } else if !has { + return nil + } + + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + if _, err := sess.Id(ou.Id).Delete(ou); err != nil { + sess.Rollback() + return err + } else if _, err = sess.Exec("UPDATE `user` SET num_members = num_members - 1 WHERE id = ?", orgId); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() +} + // ___________ ____ ___ // \__ ___/___ _____ _____ | | \______ ___________ // | |_/ __ \\__ \ / \| | / ___// __ \_ __ \ diff --git a/models/user.go b/models/user.go index f4526b51d1..757c290b9c 100644 --- a/models/user.go +++ b/models/user.go @@ -128,6 +128,16 @@ func (u *User) IsOrganization() bool { return u.Type == ORGANIZATION } +// IsUserOrgOwner returns true if user is in the owner team of given organization. +func (u *User) IsUserOrgOwner(orgId int64) bool { + return IsOrganizationOwner(orgId, u.Id) +} + +// IsPublicMember returns true if user public his/her membership in give organization. +func (u *User) IsPublicMember(orgId int64) bool { + return IsPublicMembership(orgId, u.Id) +} + // GetOrganizationCount returns count of membership of organization of user. func (u *User) GetOrganizationCount() (int64, error) { return x.Where("uid=?", u.Id).Count(new(OrgUser)) |