mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
enhance quota support.
now includes: - perOrg (users, dashboards, datasources, api_keys) - perUser (orgs) - global (users, orgs, dashboards, datasources, api_keys, sessions)
This commit is contained in:
@@ -14,14 +14,14 @@ func Register(r *macaron.Macaron) {
|
||||
reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
|
||||
reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
|
||||
regOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
|
||||
limitQuota := middleware.LimitQuota
|
||||
quota := middleware.Quota
|
||||
bind := binding.Bind
|
||||
|
||||
// not logged in views
|
||||
r.Get("/", reqSignedIn, Index)
|
||||
r.Get("/logout", Logout)
|
||||
r.Post("/login", bind(dtos.LoginCommand{}), wrap(LoginPost))
|
||||
r.Get("/login/:name", OAuthLogin)
|
||||
r.Post("/login", quota("session"), bind(dtos.LoginCommand{}), wrap(LoginPost))
|
||||
r.Get("/login/:name", quota("session"), OAuthLogin)
|
||||
r.Get("/login", LoginView)
|
||||
r.Get("/invite/:code", Index)
|
||||
|
||||
@@ -45,7 +45,7 @@ func Register(r *macaron.Macaron) {
|
||||
// sign up
|
||||
r.Get("/signup", Index)
|
||||
r.Get("/api/user/signup/options", wrap(GetSignUpOptions))
|
||||
r.Post("/api/user/signup", bind(dtos.SignUpForm{}), wrap(SignUp))
|
||||
r.Post("/api/user/signup", quota("user"), bind(dtos.SignUpForm{}), wrap(SignUp))
|
||||
r.Post("/api/user/signup/step2", bind(dtos.SignUpStep2Form{}), wrap(SignUpStep2))
|
||||
|
||||
// invited
|
||||
@@ -67,7 +67,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Get("/api/snapshots-delete/:key", DeleteDashboardSnapshot)
|
||||
|
||||
// api renew session based on remember cookie
|
||||
r.Get("/api/login/ping", LoginApiPing)
|
||||
r.Get("/api/login/ping", quota("session"), LoginApiPing)
|
||||
|
||||
// authed api
|
||||
r.Group("/api", func() {
|
||||
@@ -81,6 +81,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Post("/stars/dashboard/:id", wrap(StarDashboard))
|
||||
r.Delete("/stars/dashboard/:id", wrap(UnstarDashboard))
|
||||
r.Put("/password", bind(m.ChangeUserPasswordCommand{}), wrap(ChangeUserPassword))
|
||||
r.Get("/quotas", wrap(GetUserQuotas))
|
||||
})
|
||||
|
||||
// users (admin permission required)
|
||||
@@ -94,26 +95,26 @@ func Register(r *macaron.Macaron) {
|
||||
// org information available to all users.
|
||||
r.Group("/org", func() {
|
||||
r.Get("/", wrap(GetOrgCurrent))
|
||||
r.Get("/quotas", wrap(GetQuotas))
|
||||
r.Get("/quotas", wrap(GetOrgQuotas))
|
||||
})
|
||||
|
||||
// current org
|
||||
r.Group("/org", func() {
|
||||
r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrgCurrent))
|
||||
r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddressCurrent))
|
||||
r.Post("/users", limitQuota(m.QUOTA_USER), bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
|
||||
r.Post("/users", quota("user"), bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
|
||||
r.Get("/users", wrap(GetOrgUsersForCurrentOrg))
|
||||
r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUserForCurrentOrg))
|
||||
r.Delete("/users/:userId", wrap(RemoveOrgUserForCurrentOrg))
|
||||
|
||||
// invites
|
||||
r.Get("/invites", wrap(GetPendingOrgInvites))
|
||||
r.Post("/invites", bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
|
||||
r.Post("/invites", quota("user"), bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
|
||||
r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
|
||||
}, regOrgAdmin)
|
||||
|
||||
// create new org
|
||||
r.Post("/orgs", bind(m.CreateOrgCommand{}), wrap(CreateOrg))
|
||||
r.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
|
||||
|
||||
// search all orgs
|
||||
r.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
|
||||
@@ -129,20 +130,20 @@ func Register(r *macaron.Macaron) {
|
||||
r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUser))
|
||||
r.Delete("/users/:userId", wrap(RemoveOrgUser))
|
||||
r.Get("/quotas", wrap(GetOrgQuotas))
|
||||
r.Put("/quotas/:target", bind(m.UpdateQuotaCmd{}), wrap(UpdateOrgQuota))
|
||||
r.Put("/quotas/:target", bind(m.UpdateOrgQuotaCmd{}), wrap(UpdateOrgQuota))
|
||||
}, reqGrafanaAdmin)
|
||||
|
||||
// auth api keys
|
||||
r.Group("/auth/keys", func() {
|
||||
r.Get("/", wrap(GetApiKeys))
|
||||
r.Post("/", bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
|
||||
r.Post("/", quota("api_key"), bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
|
||||
r.Delete("/:id", wrap(DeleteApiKey))
|
||||
}, regOrgAdmin)
|
||||
|
||||
// Data sources
|
||||
r.Group("/datasources", func() {
|
||||
r.Get("/", GetDataSources)
|
||||
r.Post("/", limitQuota(m.QUOTA_DATASOURCE), bind(m.AddDataSourceCommand{}), AddDataSource)
|
||||
r.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), AddDataSource)
|
||||
r.Put("/:id", bind(m.UpdateDataSourceCommand{}), UpdateDataSource)
|
||||
r.Delete("/:id", DeleteDataSource)
|
||||
r.Get("/:id", GetDataSourceById)
|
||||
@@ -177,6 +178,8 @@ func Register(r *macaron.Macaron) {
|
||||
r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
|
||||
r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
|
||||
r.Delete("/users/:id", AdminDeleteUser)
|
||||
r.Get("/users/:id/quotas", wrap(GetUserQuotas))
|
||||
r.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
|
||||
}, reqGrafanaAdmin)
|
||||
|
||||
// rendering
|
||||
|
||||
@@ -88,7 +88,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
|
||||
|
||||
dash := cmd.GetDashboardModel()
|
||||
if dash.Id == 0 {
|
||||
limitReached, err := middleware.QuotaReached(cmd.OrgId, m.QUOTA_DASHBOARD)
|
||||
limitReached, err := middleware.QuotaReached(c, "dashboard")
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "failed to get quota", err)
|
||||
return
|
||||
|
||||
@@ -74,7 +74,15 @@ func OAuthLogin(ctx *middleware.Context) {
|
||||
ctx.Redirect(setting.AppSubUrl + "/login")
|
||||
return
|
||||
}
|
||||
|
||||
limitReached, err := middleware.QuotaReached(ctx, "user")
|
||||
if err != nil {
|
||||
ctx.Handle(500, "Failed to get user quota", err)
|
||||
return
|
||||
}
|
||||
if limitReached {
|
||||
ctx.Redirect(setting.AppSubUrl + "/login")
|
||||
return
|
||||
}
|
||||
cmd := m.CreateUserCommand{
|
||||
Login: userInfo.Email,
|
||||
Email: userInfo.Email,
|
||||
|
||||
@@ -11,7 +11,7 @@ func GetOrgQuotas(c *middleware.Context) Response {
|
||||
if !setting.Quota.Enabled {
|
||||
return ApiError(404, "Quotas not enabled", nil)
|
||||
}
|
||||
query := m.GetQuotasQuery{OrgId: c.ParamsInt64(":orgId")}
|
||||
query := m.GetOrgQuotasQuery{OrgId: c.ParamsInt64(":orgId")}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed to get org quotas", err)
|
||||
@@ -20,28 +20,44 @@ func GetOrgQuotas(c *middleware.Context) Response {
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
// allow users to query the quotas of their own org.
|
||||
func GetQuotas(c *middleware.Context) Response {
|
||||
if !setting.Quota.Enabled {
|
||||
return ApiError(404, "Quotas not enabled", nil)
|
||||
}
|
||||
query := m.GetQuotasQuery{OrgId: c.OrgId}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed to get quotas", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
func UpdateOrgQuota(c *middleware.Context, cmd m.UpdateQuotaCmd) Response {
|
||||
func UpdateOrgQuota(c *middleware.Context, cmd m.UpdateOrgQuotaCmd) Response {
|
||||
if !setting.Quota.Enabled {
|
||||
return ApiError(404, "Quotas not enabled", nil)
|
||||
}
|
||||
cmd.OrgId = c.ParamsInt64(":orgId")
|
||||
cmd.Target = m.QuotaTarget(c.Params(":target"))
|
||||
cmd.Target = c.Params(":target")
|
||||
|
||||
if !cmd.Target.IsValid() {
|
||||
if _, ok := m.QuotaToMap(setting.Quota.Org)[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 GetUserQuotas(c *middleware.Context) Response {
|
||||
if !setting.Quota.Enabled {
|
||||
return ApiError(404, "Quotas not enabled", nil)
|
||||
}
|
||||
query := m.GetUserQuotasQuery{UserId: c.ParamsInt64(":id")}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed to get org quotas", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
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 := m.QuotaToMap(setting.Quota.User)[cmd.Target]; !ok {
|
||||
return ApiError(404, "Invalid quota target", nil)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user