func GetDataSourceById(c *middleware.Context) Response { query := m.GetDataSourceByIdQuery{ Id: c.ParamsInt64(":id"), OrgId: c.OrgId, } if err := bus.Dispatch(&query); err != nil { if err == m.ErrDataSourceNotFound { return ApiError(404, "Data source not found", nil) } return ApiError(500, "Failed to query datasources", err) } ds := query.Result return Json(200, &dtos.DataSource{ Id: ds.Id, OrgId: ds.OrgId, Name: ds.Name, Url: ds.Url, Type: ds.Type, Access: ds.Access, Password: ds.Password, Database: ds.Database, User: ds.User, BasicAuth: ds.BasicAuth, BasicAuthUser: ds.BasicAuthUser, BasicAuthPassword: ds.BasicAuthPassword, WithCredentials: ds.WithCredentials, IsDefault: ds.IsDefault, JsonData: ds.JsonData, }) }
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 DeleteApiKey(c *middleware.Context) Response { id := c.ParamsInt64(":id") cmd := &m.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId} err := bus.Dispatch(cmd) if err != nil { return ApiError(500, "Failed to delete API key", err) } return ApiSuccess("API key deleted") }
func GetOrgQuotas(c *middleware.Context) Response { if !setting.Quota.Enabled { return ApiError(404, "Quotas not enabled", nil) } query := m.GetOrgQuotasQuery{OrgId: c.ParamsInt64(":orgId")} if err := bus.Dispatch(&query); err != nil { return ApiError(500, "Failed to get org quotas", err) } return Json(200, query.Result) }
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 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 UnstarDashboard(c *middleware.Context) Response { cmd := m.UnstarDashboardCommand{UserId: c.UserId, DashboardId: c.ParamsInt64(":id")} if cmd.DashboardId <= 0 { return ApiError(400, "Missing dashboard id", nil) } if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "Failed to unstar dashboard", err) } return ApiSuccess("Dashboard unstarred") }
// POST /api/user/using/:id func UserSetUsingOrg(c *middleware.Context) Response { orgId := c.ParamsInt64(":id") if !validateUsingOrg(c.UserId, orgId) { return ApiError(401, "Not a valid organization", nil) } cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId} if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "Failed change active organization", err) } return ApiSuccess("Active organization changed") }
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 UpdateUserQuota(c *middleware.Context, cmd m.UpdateUserQuotaCmd) Response { if !setting.Quota.Enabled { return ApiError(404, "Quotas not enabled", nil) } cmd.UserId = c.ParamsInt64(":id") cmd.Target = c.Params(":target") if _, ok := setting.Quota.User.ToMap()[cmd.Target]; !ok { return ApiError(404, "Invalid quota target", nil) } if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "Failed to update org quotas", err) } return ApiSuccess("Organization quota updated") }
func StarDashboard(c *middleware.Context) Response { if !c.IsSignedIn { return ApiError(412, "You need to sign in to star dashboards", nil) } cmd := m.StarDashboardCommand{UserId: c.UserId, DashboardId: c.ParamsInt64(":id")} if cmd.DashboardId <= 0 { return ApiError(400, "Missing dashboard id", nil) } if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "Failed to star dashboard", err) } return ApiSuccess("Dashboard starred!") }
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 ProxyDataSourceRequest(c *middleware.Context) { ds, err := getDatasource(c.ParamsInt64(":id"), c.OrgId) if err != nil { c.JsonApiErr(500, "Unable to load datasource meta data", err) return } targetUrl, _ := url.Parse(ds.Url) if len(setting.DataProxyWhiteList) > 0 { if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists { c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil) return } } if ds.Type == m.DS_CLOUDWATCH { cloudwatch.HandleRequest(c, ds) } else { proxyPath := c.Params("*") proxy := NewReverseProxy(ds, proxyPath, targetUrl) proxy.Transport = dataProxyTransport proxy.ServeHTTP(c.RW(), c.Req.Request) } }
// DELETE /api/org/users/:userId func RemoveOrgUserForCurrentOrg(c *middleware.Context) Response { userId := c.ParamsInt64(":userId") return removeOrgUserHelper(c.OrgId, userId) }
// PATCH /api/orgs/:orgId/users/:userId func UpdateOrgUser(c *middleware.Context, cmd m.UpdateOrgUserCommand) Response { cmd.OrgId = c.ParamsInt64(":orgId") cmd.UserId = c.ParamsInt64(":userId") return updateOrgUserHelper(cmd) }
// GET /api/orgs/:orgId/users func GetOrgUsers(c *middleware.Context) Response { return getOrgUsersHelper(c.ParamsInt64(":orgId")) }
// POST /api/orgs/:orgId/users func AddOrgUser(c *middleware.Context, cmd m.AddOrgUserCommand) Response { cmd.OrgId = c.ParamsInt64(":orgId") return addOrgUserHelper(cmd) }
// DELETE /api/orgs/:orgId/users/:userId func RemoveOrgUser(c *middleware.Context) Response { userId := c.ParamsInt64(":userId") orgId := c.ParamsInt64(":orgId") return removeOrgUserHelper(orgId, userId) }
// PUT /api/orgs/:orgId func UpdateOrg(c *middleware.Context, form dtos.UpdateOrgForm) Response { return updateOrgHelper(form, c.ParamsInt64(":orgId")) }
// GET /api/orgs/:orgId func DeleteOrgById(c *middleware.Context) Response { if err := bus.Dispatch(&m.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil { return ApiError(500, "Failed to update organization", err) } return ApiSuccess("Organization deleted") }
// GET /api/user/:id func GetUserById(c *middleware.Context) Response { return getUserUserProfile(c.ParamsInt64(":id")) }
// POST /api/users/:id func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) Response { cmd.UserId = c.ParamsInt64(":id") return handleUpdateUser(cmd) }
// GET /api/user/:id/orgs func GetUserOrgList(c *middleware.Context) Response { return getUserOrgList(c.ParamsInt64(":id")) }