mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
WIP: add update user group command
This commit is contained in:
parent
00ac446b2b
commit
0a1df0905e
@ -134,9 +134,12 @@ func (hs *HttpServer) registerRoutes() {
|
||||
|
||||
// user group (admin permission required)
|
||||
r.Group("/user-groups", func() {
|
||||
r.Get("/:userGroupId", wrap(GetUserGroupById))
|
||||
r.Get("/search", wrap(SearchUserGroups))
|
||||
r.Post("/", quota("user-groups"), bind(m.CreateUserGroupCommand{}), wrap(CreateUserGroup))
|
||||
r.Put("/:userGroupId", bind(m.UpdateUserGroupCommand{}), wrap(UpdateUserGroup))
|
||||
r.Delete("/:userGroupId", wrap(DeleteUserGroupById))
|
||||
r.Get("/:userGroupId/members", wrap(GetUserGroupMembers))
|
||||
}, reqGrafanaAdmin)
|
||||
|
||||
// org information available to all users.
|
||||
|
@ -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
|
||||
func DeleteUserGroupById(c *middleware.Context) Response {
|
||||
if err := bus.Dispatch(&m.DeleteUserGroupCommand{Id: c.ParamsInt64(":userGroupId")}); err != nil {
|
||||
@ -64,3 +77,18 @@ func SearchUserGroups(c *middleware.Context) Response {
|
||||
|
||||
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)
|
||||
}
|
||||
|
18
pkg/api/user_group_members.go
Normal file
18
pkg/api/user_group_members.go
Normal file
@ -0,0 +1,18 @@
|
||||
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 {
|
||||
Result UserGroup `json:"-"`
|
||||
}
|
||||
|
||||
type UpdateUserGroupCommand struct {
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type DeleteUserGroupCommand struct {
|
||||
Id int64
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", CreateUserGroup)
|
||||
bus.AddHandler("sql", UpdateUserGroup)
|
||||
bus.AddHandler("sql", DeleteUserGroup)
|
||||
bus.AddHandler("sql", SearchUserGroups)
|
||||
bus.AddHandler("sql", GetUserGroupById)
|
||||
@ -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 {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
if res, err := sess.Query("SELECT 1 from user_group WHERE id=?", cmd.Id); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user