func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPasswordForm) { userId := c.ParamsInt64(":id") if len(form.Password) < 4 { c.JsonApiErr(400, "New password too short", nil) return } userQuery := m.GetUserByIdQuery{Id: userId} if err := bus.Dispatch(&userQuery); err != nil { c.JsonApiErr(500, "Could not read user from database", err) return } passwordHashed := util.EncodePassword(form.Password, userQuery.Result.Salt) cmd := m.ChangeUserPasswordCommand{ UserId: userId, NewPassword: passwordHashed, } if err := bus.Dispatch(&cmd); err != nil { c.JsonApiErr(500, "Failed to update user password", err) return } c.JsonOK("User password updated") }
func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) { cmd := m.CreateUserCommand{ Login: form.Login, Email: form.Email, Password: form.Password, 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 len(cmd.Password) < 4 { c.JsonApiErr(400, "Password is missing or too short", nil) return } if err := bus.Dispatch(&cmd); err != nil { c.JsonApiErr(500, "failed to create user", err) return } metrics.M_Api_Admin_User_Create.Inc(1) c.JsonOK("User created") }
func LoginApiPing(c *middleware.Context) { if !tryLoginUsingRememberCookie(c) { c.JsonApiErr(401, "Unauthorized", nil) return } c.JsonOK("Logged in") }
func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) { cmd.OrgId = c.OrgId cmd.Id = c.ParamsInt64(":id") err := bus.Dispatch(&cmd) if err != nil { c.JsonApiErr(500, "Failed to update datasource", err) return } c.JsonOK("Datasource updated") }
func AdminDeleteUser(c *middleware.Context) { userId := c.ParamsInt64(":id") cmd := m.DeleteUserCommand{UserId: userId} if err := bus.Dispatch(&cmd); err != nil { c.JsonApiErr(500, "Failed to delete user", err) return } c.JsonOK("User deleted") }
func DeleteAlert(c *middleware.Context) { id := c.ParamsInt64(":id") cmd := &m.DeleteAlertCommand{Id: id, OrgId: c.OrgId} err := bus.Dispatch(cmd) if err != nil { c.JsonApiErr(500, "Failed to delete alert", err) return } c.JsonOK("Alert deleted") }
func LoginApiPing(c *middleware.Context) { if !tryLoginUsingRememberCookie(c) { initialized, err := netcrunch.CheckInitializationSuccess() if (err == nil) && (!initialized) { c.JsonApiErr(401, "Default credentials for fresh installation: User: admin Password: admin", nil) } else { c.JsonApiErr(401, "Unauthorized", nil) } return } c.JsonOK("Logged in") }
func AdminUpdateUserPermissions(c *middleware.Context, form dtos.AdminUpdateUserPermissionsForm) { userId := c.ParamsInt64(":id") cmd := m.UpdateUserPermissionsCommand{ UserId: userId, IsGrafanaAdmin: form.IsGrafanaAdmin, } if err := bus.Dispatch(&cmd); err != nil { c.JsonApiErr(500, "Failed to update user permissions", err) return } c.JsonOK("User permissions updated") }
func DeleteDataSource(c *middleware.Context) { id := c.ParamsInt64(":id") if id <= 0 { c.JsonApiErr(400, "Missing valid datasource id", nil) return } cmd := &m.DeleteDataSourceCommand{Id: id, OrgId: c.OrgId} err := bus.Dispatch(cmd) if err != nil { c.JsonApiErr(500, "Failed to delete datasource", err) return } c.JsonOK("Data source deleted") }
func (lc *LiveConn) PushToStream(c *middleware.Context, message dtos.StreamMessage) { h.streamChannel <- &message c.JsonOK("Message recevived") }