mirror of
https://github.com/grafana/grafana.git
synced 2025-09-28 20:13:53 +08:00
More refactoring of user http api, trying to reuse handlers for sign in user and admin operations
This commit is contained in:
@ -19,26 +19,6 @@ func AdminSearchUsers(c *middleware.Context) {
|
|||||||
c.JSON(200, query.Result)
|
c.JSON(200, query.Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AdminGetUser(c *middleware.Context) {
|
|
||||||
userId := c.ParamsInt64(":id")
|
|
||||||
|
|
||||||
query := m.GetUserByIdQuery{Id: userId}
|
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
|
||||||
c.JsonApiErr(500, "Failed to fetch user", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
result := dtos.AdminUserListItem{
|
|
||||||
Name: query.Result.Name,
|
|
||||||
Email: query.Result.Email,
|
|
||||||
Login: query.Result.Login,
|
|
||||||
IsGrafanaAdmin: query.Result.IsAdmin,
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(200, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) {
|
func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) {
|
||||||
cmd := m.CreateUserCommand{
|
cmd := m.CreateUserCommand{
|
||||||
Login: form.Login,
|
Login: form.Login,
|
||||||
@ -70,32 +50,6 @@ func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) {
|
|||||||
c.JsonOK("User created")
|
c.JsonOK("User created")
|
||||||
}
|
}
|
||||||
|
|
||||||
func AdminUpdateUser(c *middleware.Context, form dtos.AdminUpdateUserForm) {
|
|
||||||
userId := c.ParamsInt64(":id")
|
|
||||||
|
|
||||||
cmd := m.UpdateUserCommand{
|
|
||||||
UserId: userId,
|
|
||||||
Login: form.Login,
|
|
||||||
Email: form.Email,
|
|
||||||
Name: form.Name,
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(cmd.Login) == 0 {
|
|
||||||
cmd.Login = cmd.Email
|
|
||||||
if len(cmd.Login) == 0 {
|
|
||||||
c.JsonApiErr(400, "Validation error, need specify either username or email", nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
|
||||||
c.JsonApiErr(500, "failed to update user", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JsonOK("User updated")
|
|
||||||
}
|
|
||||||
|
|
||||||
func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPasswordForm) {
|
func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPasswordForm) {
|
||||||
userId := c.ParamsInt64(":id")
|
userId := c.ParamsInt64(":id")
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ func Register(r *macaron.Macaron) {
|
|||||||
// user
|
// user
|
||||||
r.Group("/user", func() {
|
r.Group("/user", func() {
|
||||||
r.Get("/", wrap(GetSignedInUser))
|
r.Get("/", wrap(GetSignedInUser))
|
||||||
r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser)
|
r.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser))
|
||||||
r.Post("/using/:id", UserSetUsingOrg)
|
r.Post("/using/:id", UserSetUsingOrg)
|
||||||
r.Get("/orgs", wrap(GetSignedInUserOrgList))
|
r.Get("/orgs", wrap(GetSignedInUserOrgList))
|
||||||
r.Post("/stars/dashboard/:id", StarDashboard)
|
r.Post("/stars/dashboard/:id", StarDashboard)
|
||||||
@ -66,8 +66,9 @@ func Register(r *macaron.Macaron) {
|
|||||||
|
|
||||||
// users
|
// users
|
||||||
r.Group("/users", func() {
|
r.Group("/users", func() {
|
||||||
r.Get("/:id/", wrap(GetUserById))
|
r.Get("/:id", wrap(GetUserById))
|
||||||
r.Get("/:id/org", wrap(GetUserOrgList))
|
r.Get("/:id/org", wrap(GetUserOrgList))
|
||||||
|
r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
|
||||||
}, reqGrafanaAdmin)
|
}, reqGrafanaAdmin)
|
||||||
|
|
||||||
// account
|
// account
|
||||||
@ -122,9 +123,7 @@ func Register(r *macaron.Macaron) {
|
|||||||
r.Group("/api/admin", func() {
|
r.Group("/api/admin", func() {
|
||||||
r.Get("/settings", AdminGetSettings)
|
r.Get("/settings", AdminGetSettings)
|
||||||
r.Get("/users", AdminSearchUsers)
|
r.Get("/users", AdminSearchUsers)
|
||||||
r.Get("/users/:id", AdminGetUser)
|
|
||||||
r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
|
r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
|
||||||
r.Put("/users/:id/details", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser)
|
|
||||||
r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
|
r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
|
||||||
r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
|
r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
|
||||||
r.Delete("/users/:id", AdminDeleteUser)
|
r.Delete("/users/:id", AdminDeleteUser)
|
||||||
|
@ -26,12 +26,17 @@ type NormalResponse struct {
|
|||||||
header http.Header
|
header http.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
func wrap(action func(c *middleware.Context) Response) macaron.Handler {
|
func wrap(action interface{}) macaron.Handler {
|
||||||
|
|
||||||
return func(c *middleware.Context) {
|
return func(c *middleware.Context) {
|
||||||
res := action(c)
|
var res Response
|
||||||
if res == nil {
|
val, err := c.Invoke(action)
|
||||||
|
if err == nil && val != nil && len(val) > 0 {
|
||||||
|
res = val[0].Interface().(Response)
|
||||||
|
} else {
|
||||||
res = ServerError
|
res = ServerError
|
||||||
}
|
}
|
||||||
|
|
||||||
res.WriteTo(c.Resp)
|
res.WriteTo(c.Resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,6 +69,12 @@ func Json(status int, body interface{}) *NormalResponse {
|
|||||||
return Respond(status, body).Header("Content-Type", "application/json")
|
return Respond(status, body).Header("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ApiSuccess(message string) *NormalResponse {
|
||||||
|
resp := make(map[string]interface{})
|
||||||
|
resp["message"] = message
|
||||||
|
return Respond(200, resp)
|
||||||
|
}
|
||||||
|
|
||||||
func ApiError(status int, message string, err error) *NormalResponse {
|
func ApiError(status int, message string, err error) *NormalResponse {
|
||||||
resp := make(map[string]interface{})
|
resp := make(map[string]interface{})
|
||||||
|
|
||||||
|
@ -27,15 +27,31 @@ func getUserUserProfile(userId int64) Response {
|
|||||||
return Json(200, query.Result)
|
return Json(200, query.Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) {
|
// POST /api/user
|
||||||
|
func UpdateSignedInUser(c *middleware.Context, cmd m.UpdateUserCommand) Response {
|
||||||
cmd.UserId = c.UserId
|
cmd.UserId = c.UserId
|
||||||
|
return handleUpdateUser(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
// POST /api/users/:id
|
||||||
c.JsonApiErr(400, "Failed to update user", err)
|
func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) Response {
|
||||||
return
|
cmd.UserId = c.ParamsInt64(":id")
|
||||||
|
return handleUpdateUser(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUpdateUser(cmd m.UpdateUserCommand) Response {
|
||||||
|
if len(cmd.Login) == 0 {
|
||||||
|
cmd.Login = cmd.Email
|
||||||
|
if len(cmd.Login) == 0 {
|
||||||
|
return ApiError(400, "Validation error, need specify either username or email", nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JsonOK("User updated")
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
|
return ApiError(500, "failed to update user", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ApiSuccess("User updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/user/orgs
|
// GET /api/user/orgs
|
||||||
|
@ -235,6 +235,7 @@ func GetUserProfile(query *m.GetUserProfileQuery) error {
|
|||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
Login: user.Login,
|
Login: user.Login,
|
||||||
Theme: user.Theme,
|
Theme: user.Theme,
|
||||||
|
IsGrafanaAdmin: user.IsAdmin,
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
@ -17,7 +17,7 @@ function (angular) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
$scope.getUser = function(id) {
|
$scope.getUser = function(id) {
|
||||||
backendSrv.get('/api/admin/users/' + id).then(function(user) {
|
backendSrv.get('/api/users/' + id).then(function(user) {
|
||||||
$scope.user = user;
|
$scope.user = user;
|
||||||
$scope.user_id = id;
|
$scope.user_id = id;
|
||||||
$scope.permissions.isGrafanaAdmin = user.isGrafanaAdmin;
|
$scope.permissions.isGrafanaAdmin = user.isGrafanaAdmin;
|
||||||
@ -52,7 +52,7 @@ function (angular) {
|
|||||||
$scope.update = function() {
|
$scope.update = function() {
|
||||||
if (!$scope.userForm.$valid) { return; }
|
if (!$scope.userForm.$valid) { return; }
|
||||||
|
|
||||||
backendSrv.put('/api/admin/users/' + $scope.user_id + '/details', $scope.user).then(function() {
|
backendSrv.put('/api/users/' + $scope.user_id, $scope.user).then(function() {
|
||||||
$location.path('/admin/users');
|
$location.path('/admin/users');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user