From 5dbfe3c26c3dd227e287d52134e978084d5081e3 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 28 Jun 2014 15:43:25 -0400 Subject: [PATCH] Finish organization homepage --- cmd/web.go | 2 +- models/org.go | 41 +++++++++++++++--- models/user.go | 9 +++- routers/org/org.go | 38 +++++++++++++++-- routers/repo/repo.go | 20 ++++++++- routers/user/home.go | 6 +++ templates/org/home.tmpl | 70 +++++++++++++++++++++++++++++++ templates/org/org.tmpl | 85 -------------------------------------- templates/repo/create.tmpl | 17 +++----- 9 files changed, 179 insertions(+), 109 deletions(-) create mode 100644 templates/org/home.tmpl delete mode 100644 templates/org/org.tmpl diff --git a/cmd/web.go b/cmd/web.go index 7387d445e..af92b7138 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -191,7 +191,7 @@ func runWeb(*cli.Context) { m.Group("/org", func(r martini.Router) { r.Get("/create", org.New) r.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.NewPost) - r.Get("/:org", org.Organization) + r.Get("/:org", org.Home) r.Get("/:org/dashboard", org.Dashboard) r.Get("/:org/members", org.Members) diff --git a/models/org.go b/models/org.go index 025759b00..29a5ac3ca 100644 --- a/models/org.go +++ b/models/org.go @@ -20,6 +20,28 @@ func (org *User) GetOwnerTeam() (*Team, error) { return t, err } +// GetTeams returns all teams that belong to organization. +func (org *User) GetTeams() error { + return x.Where("org_id=?", org.Id).Find(&org.Teams) +} + +// GetMembers returns all members of organization. +func (org *User) GetMembers() error { + ous, err := GetOrgUsersByOrgId(org.Id) + if err != nil { + return err + } + + org.Members = make([]*User, len(ous)) + for i, ou := range ous { + org.Members[i], err = GetUserById(ou.Uid) + if err != nil { + return err + } + } + return nil +} + // CreateOrganization creates record of a new organization. func CreateOrganization(org, owner *User) (*User, error) { if !IsLegalName(org.Name) { @@ -132,12 +154,13 @@ const ( ORG_ADMIN ) -const OWNER_TEAM = "Owner" +const OWNER_TEAM = "Owners" // Team represents a organization team. type Team struct { Id int64 OrgId int64 `xorm:"INDEX"` + LowerName string Name string Description string Authorize AuthorizeType @@ -148,15 +171,19 @@ type Team struct { // NewTeam creates a record of new team. func NewTeam(t *Team) error { + // TODO: check if same name team of organization exists. + t.LowerName = strings.ToLower(t.Name) _, err := x.Insert(t) return err } +// UpdateTeam updates information of team. func UpdateTeam(t *Team) error { if len(t.Description) > 255 { t.Description = t.Description[:255] } + t.LowerName = strings.ToLower(t.Name) _, err := x.Id(t.Id).AllCols().Update(t) return err } @@ -192,16 +219,18 @@ func GetOrgUsersByOrgId(orgId int64) ([]*OrgUser, error) { return ous, err } -func GetOrganizationCount(u *User) (int64, error) { - return x.Where("uid=?", u.Id).Count(new(OrgUser)) -} - -// IsOrganizationOwner returns true if given user ID is in the owner team. +// IsOrganizationOwner returns true if given user is in the owner team. func IsOrganizationOwner(orgId, uid int64) bool { has, _ := x.Where("is_owner=?", true).Get(&OrgUser{Uid: uid, OrgId: orgId}) return has } +// IsOrganizationMember returns true if given user is member of organization. +func IsOrganizationMember(orgId, uid int64) bool { + has, _ := x.Get(&OrgUser{Uid: uid, OrgId: orgId}) + return has +} + // ___________ ____ ___ // \__ ___/___ _____ _____ | | \______ ___________ // | |_/ __ \\__ \ / \| | / ___// __ \_ __ \ diff --git a/models/user.go b/models/user.go index 9b0bdebe6..d273d57f9 100644 --- a/models/user.go +++ b/models/user.go @@ -73,6 +73,8 @@ type User struct { Description string NumTeams int NumMembers int + Teams []*Team `xorm:"-"` + Members []*User `xorm:"-"` } // HomeLink returns the user home page link. @@ -110,6 +112,11 @@ func (u *User) IsOrganization() bool { return u.Type == ORGANIZATION } +// GetOrganizationCount returns count of membership of organization of user. +func (u *User) GetOrganizationCount() (int64, error) { + return x.Where("uid=?", u.Id).Count(new(OrgUser)) +} + // GetOrganizations returns all organizations that user belongs to. func (u *User) GetOrganizations() error { ous, err := GetOrgUsersByUserId(u.Id) @@ -331,7 +338,7 @@ func DeleteUser(u *User) error { } // Check membership of organization. - count, err = GetOrganizationCount(u) + count, err = u.GetOrganizationCount() if err != nil { return errors.New("modesl.GetRepositories(GetOrganizationCount): " + err.Error()) } else if count > 0 { diff --git a/routers/org/org.go b/routers/org/org.go index 7b2c4d732..ba4adc5b5 100644 --- a/routers/org/org.go +++ b/routers/org/org.go @@ -16,13 +16,45 @@ import ( ) const ( + HOME base.TplName = "org/home" NEW base.TplName = "org/new" SETTINGS base.TplName = "org/settings" ) -func Organization(ctx *middleware.Context, params martini.Params) { +func Home(ctx *middleware.Context, params martini.Params) { ctx.Data["Title"] = "Organization " + params["org"] - ctx.HTML(200, "org/org") + + org, err := models.GetUserByName(params["org"]) + if err != nil { + if err == models.ErrUserNotExist { + ctx.Handle(404, "org.Home(GetUserByName)", err) + } else { + ctx.Handle(500, "org.Home(GetUserByName)", err) + } + return + } + ctx.Data["Org"] = org + + ctx.Data["Repos"], err = models.GetRepositories(org.Id, + ctx.IsSigned && models.IsOrganizationMember(org.Id, ctx.User.Id)) + if err != nil { + ctx.Handle(500, "org.Home(GetRepositories)", err) + return + } + + if err = org.GetMembers(); err != nil { + ctx.Handle(500, "org.Home(GetMembers)", err) + return + } + ctx.Data["Members"] = org.Members + + if err = org.GetTeams(); err != nil { + ctx.Handle(500, "org.Home(GetTeams)", err) + return + } + ctx.Data["Teams"] = org.Teams + + ctx.HTML(200, HOME) } func Members(ctx *middleware.Context, params martini.Params) { @@ -63,7 +95,7 @@ func NewPost(ctx *middleware.Context, form auth.CreateOrgForm) { ctx.Data["Err_OrgName"] = true ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), NEW, &form) default: - ctx.Handle(500, "user.NewPost(CreateUser)", err) + ctx.Handle(500, "org.NewPost(CreateUser)", err) } return } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 6cb6c0660..54e5fcfb2 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -37,11 +37,23 @@ func Create(ctx *middleware.Context) { ctx.Data["LanguageIgns"] = models.LanguageIgns ctx.Data["Licenses"] = models.Licenses + ctxUser := ctx.User + orgId, _ := base.StrTo(ctx.Query("org")).Int64() + if orgId > 0 { + org, err := models.GetUserById(orgId) + if err != nil && err != models.ErrUserNotExist { + ctx.Handle(500, "home.Dashboard(GetUserById)", err) + return + } + ctxUser = org + } + ctx.Data["ContextUser"] = ctxUser + if err := ctx.User.GetOrganizations(); err != nil { ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) return } - ctx.Data["Orgs"] = ctx.User.Orgs + ctx.Data["AllUsers"] = append([]*models.User{ctx.User}, ctx.User.Orgs...) ctx.HTML(200, CREATE) } @@ -76,6 +88,12 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { } return } + + // Check ownership of organization. + if !models.IsOrganizationOwner(u.Id, ctx.User.Id) { + ctx.Error(403) + return + } } repo, err := models.CreateRepository(u, form.RepoName, form.Description, diff --git a/routers/user/home.go b/routers/user/home.go index 86907b5a9..02dc1de15 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -83,6 +83,12 @@ func Profile(ctx *middleware.Context, params martini.Params) { } return } + + if u.IsOrganization() { + ctx.Redirect("/org/" + u.Name) + return + } + // For security reason, hide e-mail address for anonymous visitors. if !ctx.IsSigned { u.Email = "" diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl new file mode 100644 index 000000000..a3c232623 --- /dev/null +++ b/templates/org/home.tmpl @@ -0,0 +1,70 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
+
+
+ +
+

{{.Org.FullName}}

+ {{if .Org.Description}}

{{.Org.Description}}

{{end}} + +
+
+
+
+
+
+
+ +
+
+ {{range .Repos}} +
+
+ +
+

{{.Name}}

+

{{.Description}}

+

Updated {{TimeSince .Updated}}

+
+ {{end}} +
+
+ +
+
+ +
+ {{range .Members}} + + {{end}} +
+
+ +
+
+
+{{template "base/footer" .}} diff --git a/templates/org/org.tmpl b/templates/org/org.tmpl deleted file mode 100644 index 872e50be8..000000000 --- a/templates/org/org.tmpl +++ /dev/null @@ -1,85 +0,0 @@ -{{template "base/head" .}} -{{template "base/navbar" .}} -
-
-
- -
-

Organization Name

-

Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language.

- -
-
-
-
-
-
-
-
- -
-
-
-
-
-
    -
  • Go
  • -
  • 6
  • -
  • 2
  • -
-
-

gogs

-

Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language.

-

Updated 17 hours ago

-
-
-
-
    -
  • Go
  • -
  • 6
  • -
  • 2
  • -
-
-

gogs

-

Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language.

-

Updated 17 hours ago

-
-
-
- -
-
-{{template "base/footer" .}} diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 38b32a3bf..ed01d9772 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -10,23 +10,16 @@
- +