Commit 0a1df090 by Daniel Lee

WIP: add update user group command

parent 00ac446b
...@@ -134,9 +134,12 @@ func (hs *HttpServer) registerRoutes() { ...@@ -134,9 +134,12 @@ func (hs *HttpServer) registerRoutes() {
// user group (admin permission required) // user group (admin permission required)
r.Group("/user-groups", func() { r.Group("/user-groups", func() {
r.Get("/:userGroupId", wrap(GetUserGroupById))
r.Get("/search", wrap(SearchUserGroups)) r.Get("/search", wrap(SearchUserGroups))
r.Post("/", quota("user-groups"), bind(m.CreateUserGroupCommand{}), wrap(CreateUserGroup)) r.Post("/", quota("user-groups"), bind(m.CreateUserGroupCommand{}), wrap(CreateUserGroup))
r.Put("/:userGroupId", bind(m.UpdateUserGroupCommand{}), wrap(UpdateUserGroup))
r.Delete("/:userGroupId", wrap(DeleteUserGroupById)) r.Delete("/:userGroupId", wrap(DeleteUserGroupById))
r.Get("/:userGroupId/members", wrap(GetUserGroupMembers))
}, reqGrafanaAdmin) }, reqGrafanaAdmin)
// org information available to all users. // org information available to all users.
......
...@@ -26,6 +26,19 @@ func CreateUserGroup(c *middleware.Context, cmd m.CreateUserGroupCommand) Respon ...@@ -26,6 +26,19 @@ func CreateUserGroup(c *middleware.Context, cmd m.CreateUserGroupCommand) Respon
}) })
} }
// PUT /api/user-groups/:userGroupId
func UpdateUserGroup(c *middleware.Context, cmd m.UpdateUserGroupCommand) Response {
cmd.Id = c.ParamsInt64(":userGroupId")
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrUserGroupNameTaken {
return ApiError(400, "User Group name taken", err)
}
return ApiError(500, "Failed to update User Group", err)
}
return ApiSuccess("User Group updated")
}
// DELETE /api/user-groups/:userGroupId // DELETE /api/user-groups/:userGroupId
func DeleteUserGroupById(c *middleware.Context) Response { func DeleteUserGroupById(c *middleware.Context) Response {
if err := bus.Dispatch(&m.DeleteUserGroupCommand{Id: c.ParamsInt64(":userGroupId")}); err != nil { if err := bus.Dispatch(&m.DeleteUserGroupCommand{Id: c.ParamsInt64(":userGroupId")}); err != nil {
...@@ -64,3 +77,18 @@ func SearchUserGroups(c *middleware.Context) Response { ...@@ -64,3 +77,18 @@ func SearchUserGroups(c *middleware.Context) Response {
return Json(200, query.Result) return Json(200, query.Result)
} }
// GET /api/user-groups/:userGroupId
func GetUserGroupById(c *middleware.Context) Response {
query := m.GetUserGroupByIdQuery{Id: c.ParamsInt64(":userGroupId")}
if err := bus.Dispatch(&query); err != nil {
if err == m.ErrUserGroupNotFound {
return ApiError(404, "User Group not found", err)
}
return ApiError(500, "Failed to get User Group", err)
}
return Json(200, &query.Result)
}
package api
import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
)
// GET /api/user-groups/:userGroupId/members
func GetUserGroupMembers(c *middleware.Context) Response {
query := m.GetUserGroupMembersQuery{UserGroupId: c.ParamsInt64(":userGroupId")}
if err := bus.Dispatch(&query); err != nil {
return ApiError(500, "Failed to get User Group Members", err)
}
return Json(200, query.Result)
}
...@@ -31,6 +31,11 @@ type CreateUserGroupCommand struct { ...@@ -31,6 +31,11 @@ type CreateUserGroupCommand struct {
Result UserGroup `json:"-"` Result UserGroup `json:"-"`
} }
type UpdateUserGroupCommand struct {
Id int64
Name string
}
type DeleteUserGroupCommand struct { type DeleteUserGroupCommand struct {
Id int64 Id int64
} }
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
func init() { func init() {
bus.AddHandler("sql", CreateUserGroup) bus.AddHandler("sql", CreateUserGroup)
bus.AddHandler("sql", UpdateUserGroup)
bus.AddHandler("sql", DeleteUserGroup) bus.AddHandler("sql", DeleteUserGroup)
bus.AddHandler("sql", SearchUserGroups) bus.AddHandler("sql", SearchUserGroups)
bus.AddHandler("sql", GetUserGroupById) bus.AddHandler("sql", GetUserGroupById)
...@@ -44,6 +45,34 @@ func CreateUserGroup(cmd *m.CreateUserGroupCommand) error { ...@@ -44,6 +45,34 @@ func CreateUserGroup(cmd *m.CreateUserGroupCommand) error {
}) })
} }
func UpdateUserGroup(cmd *m.UpdateUserGroupCommand) error {
return inTransaction2(func(sess *session) error {
if isNameTaken, err := isUserGroupNameTaken(cmd.Name, cmd.Id, sess); err != nil {
return err
} else if isNameTaken {
return m.ErrUserGroupNameTaken
}
userGroup := m.UserGroup{
Name: cmd.Name,
Updated: time.Now(),
}
affectedRows, err := sess.Id(cmd.Id).Update(&userGroup)
if err != nil {
return err
}
if affectedRows == 0 {
return m.ErrUserGroupNotFound
}
return nil
})
}
func DeleteUserGroup(cmd *m.DeleteUserGroupCommand) error { func DeleteUserGroup(cmd *m.DeleteUserGroupCommand) error {
return inTransaction2(func(sess *session) error { return inTransaction2(func(sess *session) error {
if res, err := sess.Query("SELECT 1 from user_group WHERE id=?", cmd.Id); err != nil { if res, err := sess.Query("SELECT 1 from user_group WHERE id=?", cmd.Id); err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment