Commit 55293503 by Daniel Lee

WIP: API methods for add/remove members to user group

parent 0a1df090
......@@ -140,7 +140,9 @@ func (hs *HttpServer) registerRoutes() {
r.Put("/:userGroupId", bind(m.UpdateUserGroupCommand{}), wrap(UpdateUserGroup))
r.Delete("/:userGroupId", wrap(DeleteUserGroupById))
r.Get("/:userGroupId/members", wrap(GetUserGroupMembers))
}, reqGrafanaAdmin)
r.Post("/:userGroupId/members", quota("user-groups"), bind(m.AddUserGroupMemberCommand{}), wrap(AddUserGroupMember))
r.Delete("/:userGroupId/members/:userId", wrap(RemoveUserGroupMember))
}, reqOrgAdmin)
// org information available to all users.
r.Group("/org", func() {
......
......@@ -4,6 +4,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/util"
)
// GET /api/user-groups/:userGroupId/members
......@@ -16,3 +17,28 @@ func GetUserGroupMembers(c *middleware.Context) Response {
return Json(200, query.Result)
}
// POST /api/user-groups/:userGroupId/members
func AddUserGroupMember(c *middleware.Context, cmd m.AddUserGroupMemberCommand) Response {
cmd.UserGroupId = c.ParamsInt64(":userGroupId")
cmd.OrgId = c.OrgId
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrUserGroupMemberAlreadyAdded {
return ApiError(400, "User is already added to this user group", err)
}
return ApiError(500, "Failed to add Member to User Group", err)
}
return Json(200, &util.DynMap{
"message": "Member added to User Group",
})
}
// DELETE /api/user-groups/:userGroupId/members/:userId
func RemoveUserGroupMember(c *middleware.Context) Response {
if err := bus.Dispatch(&m.RemoveUserGroupMemberCommand{UserGroupId: c.ParamsInt64(":userGroupId"), UserId: c.ParamsInt64(":userId")}); err != nil {
return ApiError(500, "Failed to remove Member from User Group", err)
}
return ApiSuccess("User Group Member removed")
}
......@@ -25,9 +25,9 @@ type UserGroupMember struct {
// COMMANDS
type AddUserGroupMemberCommand struct {
UserId int64 `json:"userId" binding:"Required"`
OrgId int64 `json:"-"`
UserGroupId int64 `json:"-"`
UserId int64 `json:"-"`
}
type RemoveUserGroupMemberCommand struct {
......
......@@ -33,6 +33,7 @@ func CreateUserGroup(cmd *m.CreateUserGroupCommand) error {
userGroup := m.UserGroup{
Name: cmd.Name,
OrgId: cmd.OrgId,
Created: time.Now(),
Updated: time.Now(),
}
......
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