summaryrefslogtreecommitdiffstats
path: root/models/org.go
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-06-27 03:37:01 -0400
committerUnknown <joe2010xtmf@163.com>2014-06-27 03:37:01 -0400
commit19e910428951135b9a341554dad54a6546d2ad50 (patch)
treefde86e17d1cfccd3fb42b1f57f0f1eb9f0f9b9be /models/org.go
parent32b09681b2da96afe9a44790e7944c4ac51e696c (diff)
downloadgitea-19e910428951135b9a341554dad54a6546d2ad50.tar.gz
gitea-19e910428951135b9a341554dad54a6546d2ad50.zip
Organization settings page
Diffstat (limited to 'models/org.go')
-rw-r--r--models/org.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/models/org.go b/models/org.go
index 227151ab02..553a46aa0b 100644
--- a/models/org.go
+++ b/models/org.go
@@ -4,6 +4,88 @@
package models
+import (
+ "strings"
+
+ "github.com/gogits/gogs/modules/base"
+)
+
+// 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.FullName = 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_TEAM,
+ 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()
+}
+
type AuthorizeType int
const (
@@ -72,6 +154,10 @@ func GetOrgUsersByOrgId(orgId int64) ([]*OrgUser, error) {
return ous, err
}
+func GetOrganizationCount(u *User) (int64, error) {
+ return x.Where("uid=?", u.Id).Count(new(OrgUser))
+}
+
// ___________ ____ ___
// \__ ___/___ _____ _____ | | \______ ___________
// | |_/ __ \\__ \ / \| | / ___// __ \_ __ \