diff --git a/core/groups.go b/core/groups.go index fcd4d9f7..785eb9ed 100644 --- a/core/groups.go +++ b/core/groups.go @@ -41,15 +41,29 @@ func (g *Group) Services() []*Service { } // SelectGroups returns all groups -func SelectGroups() []*Group { +func SelectGroups(includeAll bool, auth bool) []*Group { var groups []*Group + var validGroups []*Group groupsDb().Find(&groups).Order("id desc") - return groups + if includeAll { + emptyGroup := &Group{&types.Group{Id: 0, Public: types.NewNullBool(true)}} + groups = append(groups, emptyGroup) + } + for _, g := range groups { + if !g.Public.Bool { + if auth { + validGroups = append(validGroups, g) + } + } else { + validGroups = append(validGroups, g) + } + } + return validGroups } // SelectGroup returns a *core.Group func SelectGroup(id int64) *Group { - for _, g := range SelectGroups() { + for _, g := range SelectGroups(false, false) { if g.Id == id { return g } diff --git a/core/sample.go b/core/sample.go index f4a72f2f..3b6dc473 100644 --- a/core/sample.go +++ b/core/sample.go @@ -103,11 +103,13 @@ func InsertSampleData() error { func insertSampleGroups() error { group1 := &Group{&types.Group{ - Name: "Main Services", + Name: "Main Services", + Public: types.NewNullBool(true), }} _, err := group1.Create() group2 := &Group{&types.Group{ - Name: "Linked Services", + Name: "Linked Services", + Public: types.NewNullBool(false), }} _, err = group2.Create() return err diff --git a/handlers/groups.go b/handlers/groups.go index bffff987..4a801b5b 100644 --- a/handlers/groups.go +++ b/handlers/groups.go @@ -24,16 +24,19 @@ import ( "net/http" ) +// apiAllGroupHandler will show all the groups func apiAllGroupHandler(w http.ResponseWriter, r *http.Request) { if !IsReadAuthenticated(r) { sendUnauthorizedJson(w, r) return } - groups := core.SelectGroups() + auth := IsUser(r) + groups := core.SelectGroups(false, auth) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(groups) } +// apiGroupHandler will show a single group func apiGroupHandler(w http.ResponseWriter, r *http.Request) { if !IsReadAuthenticated(r) { sendUnauthorizedJson(w, r) @@ -49,6 +52,7 @@ func apiGroupHandler(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(group) } +// apiCreateGroupHandler accepts a POST method to create new groups func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) { if !IsFullAuthenticated(r) { sendUnauthorizedJson(w, r) @@ -69,6 +73,7 @@ func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) { sendJsonAction(group, "create", w, r) } +// apiGroupDeleteHandler accepts a DELETE method to delete groups func apiGroupDeleteHandler(w http.ResponseWriter, r *http.Request) { if !IsFullAuthenticated(r) { sendUnauthorizedJson(w, r) diff --git a/handlers/handlers.go b/handlers/handlers.go index 2c2cd9f8..cfbb372f 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -192,8 +192,9 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap "Services": func() []types.ServiceInterface { return core.CoreApp.Services }, - "Groups": func() []*core.Group { - return core.SelectGroups() + "Groups": func(includeAll bool) []*core.Group { + auth := IsUser(r) + return core.SelectGroups(includeAll, auth) }, "len": func(g interface{}) int { val := reflect.ValueOf(g) diff --git a/handlers/services.go b/handlers/services.go index c74a8b46..5265fc03 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -53,7 +53,6 @@ func servicesHandler(w http.ResponseWriter, r *http.Request) { } data := map[string]interface{}{ "Services": core.CoreApp.Services, - "Groups": core.SelectGroups(), } ExecuteResponse(w, r, "services.gohtml", data, nil) } diff --git a/source/tmpl/form_group.gohtml b/source/tmpl/form_group.gohtml index 9badb4cd..c011bb11 100644 --- a/source/tmpl/form_group.gohtml +++ b/source/tmpl/form_group.gohtml @@ -13,7 +13,15 @@ - +