2021-08-04 20:44:37 +08:00
package api
import (
2023-12-01 22:50:55 +08:00
"context"
2022-08-05 15:19:50 +08:00
"fmt"
2022-02-18 18:27:00 +08:00
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
2023-01-27 15:50:36 +08:00
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
2022-03-10 00:57:50 +08:00
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/datasources"
2023-12-01 22:50:55 +08:00
"github.com/grafana/grafana/pkg/services/featuremgmt"
2024-08-01 23:20:38 +08:00
"github.com/grafana/grafana/pkg/services/folder"
2023-10-12 07:30:50 +08:00
"github.com/grafana/grafana/pkg/services/libraryelements"
2022-08-10 17:56:48 +08:00
"github.com/grafana/grafana/pkg/services/org"
2023-03-27 17:15:37 +08:00
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginaccesscontrol"
2022-08-05 15:19:50 +08:00
"github.com/grafana/grafana/pkg/tsdb/grafanads"
2021-08-04 20:44:37 +08:00
)
// API related actions
const (
ActionProvisioningReload = "provisioning:reload"
)
// API related scopes
2021-10-06 19:15:09 +08:00
var (
2022-02-18 18:27:00 +08:00
ScopeProvisionersAll = ac . Scope ( "provisioners" , "*" )
ScopeProvisionersDashboards = ac . Scope ( "provisioners" , "dashboards" )
ScopeProvisionersPlugins = ac . Scope ( "provisioners" , "plugins" )
ScopeProvisionersDatasources = ac . Scope ( "provisioners" , "datasources" )
ScopeProvisionersNotifications = ac . Scope ( "provisioners" , "notifications" )
2022-07-15 05:53:13 +08:00
ScopeProvisionersAlertRules = ac . Scope ( "provisioners" , "alerting" )
2021-08-04 20:44:37 +08:00
)
// declareFixedRoles declares to the AccessControl service fixed roles and their
// grants to organization roles ("Viewer", "Editor", "Admin") or "Grafana Admin"
// that HTTPServer needs
func ( hs * HTTPServer ) declareFixedRoles ( ) error {
2022-07-08 19:24:09 +08:00
// Declare plugins roles
2024-01-29 07:22:45 +08:00
if err := pluginaccesscontrol . DeclareRBACRoles ( hs . accesscontrolService , hs . Cfg , hs . Features ) ; err != nil {
2022-07-08 19:24:09 +08:00
return err
}
2022-02-18 18:27:00 +08:00
provisioningWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2021-11-17 22:40:39 +08:00
Name : "fixed:provisioning:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2021-11-17 22:40:39 +08:00
Description : "Reload provisioning." ,
2021-11-18 17:16:18 +08:00
Group : "Provisioning" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
2021-11-17 22:40:39 +08:00
{
Action : ActionProvisioningReload ,
Scope : ScopeProvisionersAll ,
2021-09-01 21:18:17 +08:00
} ,
} ,
} ,
2022-02-18 18:27:00 +08:00
Grants : [ ] string { ac . RoleGrafanaAdmin } ,
2021-11-17 22:40:39 +08:00
}
2022-02-18 18:27:00 +08:00
datasourcesExplorerRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2022-01-31 23:33:41 +08:00
Name : "fixed:datasources:explorer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Explorer" ,
2025-02-21 01:56:55 +08:00
Description : "Enable the Explore and Drilldown features. Data source permissions still apply; you can only query data sources for which you have query permissions." ,
2022-01-31 23:33:41 +08:00
Group : "Data sources" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
2022-01-31 23:33:41 +08:00
{
2022-02-18 18:27:00 +08:00
Action : ac . ActionDatasourcesExplore ,
2022-01-31 23:33:41 +08:00
} ,
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleEditor ) } ,
2022-01-31 23:33:41 +08:00
}
2025-04-02 17:25:42 +08:00
//nolint:staticcheck // ViewersCanEdit is deprecated but still used for backward compatibility
if hs . Cfg . ViewersCanEdit {
datasourcesExplorerRole . Grants = append ( datasourcesExplorerRole . Grants , string ( org . RoleViewer ) )
}
2022-02-18 18:27:00 +08:00
datasourcesReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2021-11-17 22:40:39 +08:00
Name : "fixed:datasources:reader" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2021-11-17 22:40:39 +08:00
Description : "Read and query all data sources." ,
2021-11-18 17:16:18 +08:00
Group : "Data sources" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
2021-11-17 22:40:39 +08:00
{
2022-03-16 22:11:03 +08:00
Action : datasources . ActionRead ,
Scope : datasources . ScopeAll ,
2021-11-17 22:40:39 +08:00
} ,
{
2022-03-16 22:11:03 +08:00
Action : datasources . ActionQuery ,
Scope : datasources . ScopeAll ,
2021-09-01 21:18:17 +08:00
} ,
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleAdmin ) } ,
2021-11-17 22:40:39 +08:00
}
2022-08-05 15:19:50 +08:00
builtInDatasourceReader := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:datasources.builtin:reader" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Built in data source reader" ,
2022-08-05 15:19:50 +08:00
Description : "Read and query Grafana's built in test data sources." ,
Group : "Data sources" ,
Permissions : [ ] ac . Permission {
{
Action : datasources . ActionRead ,
Scope : fmt . Sprintf ( "%s%s" , datasources . ScopePrefix , grafanads . DatasourceUID ) ,
} ,
{
Action : datasources . ActionQuery ,
Scope : fmt . Sprintf ( "%s%s" , datasources . ScopePrefix , grafanads . DatasourceUID ) ,
} ,
} ,
Hidden : true ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleViewer ) } ,
2022-08-05 15:19:50 +08:00
}
2022-05-25 19:43:58 +08:00
// when running oss or enterprise without a license all users should be able to query data sources
2022-12-02 20:19:14 +08:00
if ! hs . License . FeatureEnabled ( "dspermissions.enforcement" ) {
2022-08-10 17:56:48 +08:00
datasourcesReaderRole . Grants = [ ] string { string ( org . RoleViewer ) }
2022-05-25 19:43:58 +08:00
}
2023-10-19 21:36:41 +08:00
datasourcesCreatorRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:datasources:creator" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Creator" ,
2023-10-19 21:36:41 +08:00
Description : "Create data sources." ,
Group : "Data sources" ,
Permissions : [ ] ac . Permission {
{
Action : datasources . ActionCreate ,
} ,
} ,
} ,
Grants : [ ] string { } ,
}
2022-02-18 18:27:00 +08:00
datasourcesWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2021-11-17 22:40:39 +08:00
Name : "fixed:datasources:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2021-11-17 22:40:39 +08:00
Description : "Create, update, delete, read, or query data sources." ,
2021-11-18 17:16:18 +08:00
Group : "Data sources" ,
2022-02-18 18:27:00 +08:00
Permissions : ac . ConcatPermissions ( datasourcesReaderRole . Role . Permissions , [ ] ac . Permission {
2021-11-17 22:40:39 +08:00
{
2022-03-16 22:11:03 +08:00
Action : datasources . ActionWrite ,
Scope : datasources . ScopeAll ,
2021-08-04 20:44:37 +08:00
} ,
2021-11-17 22:40:39 +08:00
{
2022-03-16 22:11:03 +08:00
Action : datasources . ActionCreate ,
2021-11-17 22:40:39 +08:00
} ,
{
2022-03-16 22:11:03 +08:00
Action : datasources . ActionDelete ,
Scope : datasources . ScopeAll ,
2021-11-17 22:40:39 +08:00
} ,
} ) ,
2021-08-04 20:44:37 +08:00
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleAdmin ) } ,
2021-11-17 22:40:39 +08:00
}
2022-02-18 18:27:00 +08:00
datasourcesIdReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2021-11-17 22:40:39 +08:00
Name : "fixed:datasources.id:reader" ,
DisplayName : "Data source ID reader" ,
Description : "Read the ID of a data source based on its name." ,
2021-11-18 17:16:18 +08:00
Group : "Infrequently used" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
2021-11-17 22:40:39 +08:00
{
2022-03-16 22:11:03 +08:00
Action : datasources . ActionIDRead ,
Scope : datasources . ScopeAll ,
2021-10-21 21:41:40 +08:00
} ,
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleViewer ) } ,
2021-11-17 22:40:39 +08:00
}
2022-02-18 18:27:00 +08:00
orgReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2021-11-24 17:08:42 +08:00
Name : "fixed:organization:reader" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2021-11-24 17:08:42 +08:00
Description : "Read an organization, such as its ID, name, address, or quotas." ,
2021-11-18 17:16:18 +08:00
Group : "Organizations" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
2022-09-23 04:04:48 +08:00
{ Action : ac . ActionOrgsRead } ,
{ Action : ac . ActionOrgsQuotasRead } ,
2021-10-27 17:01:21 +08:00
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleViewer ) , ac . RoleGrafanaAdmin } ,
2021-11-17 22:40:39 +08:00
}
2022-02-18 18:27:00 +08:00
orgWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2021-11-24 17:08:42 +08:00
Name : "fixed:organization:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2021-11-24 17:08:42 +08:00
Description : "Read an organization, its quotas, or its preferences. Update organization properties, or its preferences." ,
2021-11-18 17:16:18 +08:00
Group : "Organizations" ,
2022-02-18 18:27:00 +08:00
Permissions : ac . ConcatPermissions ( orgReaderRole . Role . Permissions , [ ] ac . Permission {
2022-09-23 04:04:48 +08:00
{ Action : ac . ActionOrgsPreferencesRead } ,
{ Action : ac . ActionOrgsWrite } ,
{ Action : ac . ActionOrgsPreferencesWrite } ,
2021-11-17 22:40:39 +08:00
} ) ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleAdmin ) } ,
2021-11-17 22:40:39 +08:00
}
2022-02-18 18:27:00 +08:00
orgMaintainerRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2021-11-24 17:08:42 +08:00
Name : "fixed:organization:maintainer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Maintainer" ,
2021-11-24 17:08:42 +08:00
Description : "Create, read, write, or delete an organization. Read or write an organization's quotas. Needs to be assigned globally." ,
2021-11-18 17:16:18 +08:00
Group : "Organizations" ,
2022-02-18 18:27:00 +08:00
Permissions : ac . ConcatPermissions ( orgReaderRole . Role . Permissions , [ ] ac . Permission {
2022-09-23 04:04:48 +08:00
{ Action : ac . ActionOrgsCreate } ,
{ Action : ac . ActionOrgsWrite } ,
{ Action : ac . ActionOrgsDelete } ,
{ Action : ac . ActionOrgsQuotasWrite } ,
2021-11-17 22:40:39 +08:00
} ) ,
} ,
2022-02-18 18:27:00 +08:00
Grants : [ ] string { string ( ac . RoleGrafanaAdmin ) } ,
2021-08-04 20:44:37 +08:00
}
2022-08-10 17:56:48 +08:00
teamCreatorGrants := [ ] string { string ( org . RoleAdmin ) }
2025-03-13 00:25:23 +08:00
2022-02-18 18:27:00 +08:00
teamsCreatorRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2022-01-26 22:48:41 +08:00
Name : "fixed:teams:creator" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Creator" ,
2022-07-26 16:43:29 +08:00
Description : "Create teams and read organisation users (required to manage the created teams)." ,
2022-01-11 18:58:40 +08:00
Group : "Teams" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
{ Action : ac . ActionTeamsCreate } ,
{ Action : ac . ActionOrgUsersRead , Scope : ac . ScopeUsersAll } ,
2022-01-11 18:58:40 +08:00
} ,
} ,
2022-01-26 22:48:41 +08:00
Grants : teamCreatorGrants ,
2022-01-11 18:58:40 +08:00
}
2023-10-19 21:36:41 +08:00
teamsReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:teams:read" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2023-10-19 21:36:41 +08:00
Description : "List all teams." ,
Group : "Teams" ,
Permissions : [ ] ac . Permission {
{ Action : ac . ActionTeamsRead , Scope : ac . ScopeTeamsAll } ,
} ,
} ,
Grants : [ ] string { } ,
}
2022-02-18 18:27:00 +08:00
teamsWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2022-01-27 23:16:44 +08:00
Name : "fixed:teams:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2022-01-27 23:16:44 +08:00
Description : "Create, read, write, or delete a team as well as controlling team memberships." ,
Group : "Teams" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
{ Action : ac . ActionTeamsCreate } ,
{ Action : ac . ActionTeamsDelete , Scope : ac . ScopeTeamsAll } ,
{ Action : ac . ActionTeamsPermissionsRead , Scope : ac . ScopeTeamsAll } ,
{ Action : ac . ActionTeamsPermissionsWrite , Scope : ac . ScopeTeamsAll } ,
{ Action : ac . ActionTeamsRead , Scope : ac . ScopeTeamsAll } ,
{ Action : ac . ActionTeamsWrite , Scope : ac . ScopeTeamsAll } ,
2022-01-27 23:16:44 +08:00
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleAdmin ) } ,
2022-01-27 23:16:44 +08:00
}
2022-02-18 18:27:00 +08:00
annotationsReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
2022-02-12 02:43:29 +08:00
Name : "fixed:annotations:reader" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2022-02-12 02:43:29 +08:00
Description : "Read annotations and tags" ,
Group : "Annotations" ,
2022-02-18 18:27:00 +08:00
Permissions : [ ] ac . Permission {
{ Action : ac . ActionAnnotationsRead , Scope : ac . ScopeAnnotationsAll } ,
2022-02-12 02:43:29 +08:00
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleViewer ) } ,
2022-02-12 02:43:29 +08:00
}
2023-12-01 22:50:55 +08:00
// TODO this role can be removed once we have rolled out FlagAnnotationPermissionUpdate to all users
// keeping it in for now for backwards compatibility
2022-03-22 01:28:39 +08:00
dashboardAnnotationsWriterRole := ac . RoleRegistration {
2022-03-19 00:33:21 +08:00
Role : ac . RoleDTO {
2022-03-22 01:28:39 +08:00
Name : "fixed:annotations.dashboard:writer" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Writer (dashboard)" ,
2022-03-19 00:33:21 +08:00
Description : "Update annotations associated with dashboards." ,
Group : "Annotations" ,
Permissions : [ ] ac . Permission {
2022-03-24 05:39:00 +08:00
{ Action : ac . ActionAnnotationsCreate , Scope : ac . ScopeAnnotationsTypeDashboard } ,
2022-03-22 01:28:39 +08:00
{ Action : ac . ActionAnnotationsDelete , Scope : ac . ScopeAnnotationsTypeDashboard } ,
{ Action : ac . ActionAnnotationsWrite , Scope : ac . ScopeAnnotationsTypeDashboard } ,
2022-03-19 00:33:21 +08:00
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleViewer ) } ,
2022-03-19 00:33:21 +08:00
}
annotationsWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:annotations:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2022-03-19 00:33:21 +08:00
Description : "Update all annotations." ,
Group : "Annotations" ,
Permissions : [ ] ac . Permission {
2022-03-24 05:39:00 +08:00
{ Action : ac . ActionAnnotationsCreate , Scope : ac . ScopeAnnotationsAll } ,
2022-03-22 01:28:39 +08:00
{ Action : ac . ActionAnnotationsDelete , Scope : ac . ScopeAnnotationsAll } ,
2022-03-19 00:33:21 +08:00
{ Action : ac . ActionAnnotationsWrite , Scope : ac . ScopeAnnotationsAll } ,
} ,
} ,
2022-08-10 17:56:48 +08:00
Grants : [ ] string { string ( org . RoleEditor ) } ,
2022-03-19 00:33:21 +08:00
}
2023-12-01 22:50:55 +08:00
if hs . Features . IsEnabled ( context . Background ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
// Keeping the name to avoid breaking changes (for users who have assigned this role to grant permissions on organization annotations)
annotationsReaderRole = ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:annotations:reader" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Reader (organization)" ,
2023-12-01 22:50:55 +08:00
Description : "Read organization annotations and annotation tags" ,
Group : "Annotations" ,
Permissions : [ ] ac . Permission {
2023-12-12 15:51:08 +08:00
// Need to leave the permissions as they are, so that the seeder doesn't replace permissions when they have been removed from the basic role by the user
// Otherwise we could split this into ac.ScopeAnnotationsTypeOrganization and ac.ScopeAnnotationsTypeDashboard scopes and eventually remove the dashboard scope.
// https://github.com/grafana/identity-access-team/issues/524
{ Action : ac . ActionAnnotationsRead , Scope : ac . ScopeAnnotationsAll } ,
2023-12-01 22:50:55 +08:00
} ,
} ,
Grants : [ ] string { string ( org . RoleViewer ) } ,
}
// Keeping the name to avoid breaking changes (for users who have assigned this role to grant permissions on organization annotations)
annotationsWriterRole = ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:annotations:writer" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Writer (organization)" ,
2023-12-01 22:50:55 +08:00
Description : "Update organization annotations." ,
Group : "Annotations" ,
Permissions : [ ] ac . Permission {
2023-12-12 15:51:08 +08:00
// Need to leave the permissions as they are, so that the seeder doesn't replace permissions when they have been removed from the basic role by the user
// Otherwise we could split this into ac.ScopeAnnotationsTypeOrganization and ac.ScopeAnnotationsTypeDashboard scopes and eventually remove the dashboard scope.
// https://github.com/grafana/identity-access-team/issues/524
{ Action : ac . ActionAnnotationsCreate , Scope : ac . ScopeAnnotationsAll } ,
{ Action : ac . ActionAnnotationsDelete , Scope : ac . ScopeAnnotationsAll } ,
{ Action : ac . ActionAnnotationsWrite , Scope : ac . ScopeAnnotationsAll } ,
2023-12-01 22:50:55 +08:00
} ,
} ,
Grants : [ ] string { string ( org . RoleEditor ) } ,
}
}
2022-03-03 22:05:47 +08:00
dashboardsCreatorRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:dashboards:creator" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Creator" ,
2024-08-01 23:20:38 +08:00
Description : "Create dashboards under the root folder." ,
2022-03-03 22:05:47 +08:00
Group : "Dashboards" ,
Permissions : [ ] ac . Permission {
2022-03-30 21:14:26 +08:00
{ Action : dashboards . ActionFoldersRead , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
2022-05-04 22:12:09 +08:00
{ Action : dashboards . ActionDashboardsCreate , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
2022-03-03 22:05:47 +08:00
} ,
} ,
Grants : [ ] string { "Editor" } ,
}
dashboardsReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:dashboards:reader" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2022-03-03 22:05:47 +08:00
Description : "Read all dashboards." ,
Group : "Dashboards" ,
Permissions : [ ] ac . Permission {
2022-05-04 22:12:09 +08:00
{ Action : dashboards . ActionDashboardsRead , Scope : dashboards . ScopeDashboardsAll } ,
2022-03-03 22:05:47 +08:00
} ,
} ,
Grants : [ ] string { "Admin" } ,
}
dashboardsWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:dashboards:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2022-03-03 22:05:47 +08:00
Group : "Dashboards" ,
Description : "Create, read, write or delete all dashboards and their permissions." ,
Permissions : ac . ConcatPermissions ( dashboardsReaderRole . Role . Permissions , [ ] ac . Permission {
2022-05-04 22:12:09 +08:00
{ Action : dashboards . ActionDashboardsWrite , Scope : dashboards . ScopeDashboardsAll } ,
{ Action : dashboards . ActionDashboardsDelete , Scope : dashboards . ScopeDashboardsAll } ,
{ Action : dashboards . ActionDashboardsCreate , Scope : dashboards . ScopeFoldersAll } ,
{ Action : dashboards . ActionDashboardsPermissionsRead , Scope : dashboards . ScopeDashboardsAll } ,
{ Action : dashboards . ActionDashboardsPermissionsWrite , Scope : dashboards . ScopeDashboardsAll } ,
2022-03-03 22:05:47 +08:00
} ) ,
} ,
Grants : [ ] string { "Admin" } ,
}
foldersCreatorRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:folders:creator" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Creator" ,
2024-08-01 23:20:38 +08:00
Description : "Create folders under root level" ,
2022-03-03 22:05:47 +08:00
Group : "Folders" ,
Permissions : [ ] ac . Permission {
2024-08-01 23:20:38 +08:00
{ Action : dashboards . ActionFoldersCreate , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( folder . GeneralFolderUID ) } ,
2022-03-03 22:05:47 +08:00
} ,
} ,
Grants : [ ] string { "Editor" } ,
2024-08-08 20:11:17 +08:00
// Don't grant fixed:folders:creator to Admin
Exclude : [ ] string { "Admin" } ,
2022-03-03 22:05:47 +08:00
}
foldersReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:folders:reader" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2022-03-03 22:05:47 +08:00
Description : "Read all folders and dashboards." ,
Group : "Folders" ,
Permissions : [ ] ac . Permission {
2022-03-10 00:57:50 +08:00
{ Action : dashboards . ActionFoldersRead , Scope : dashboards . ScopeFoldersAll } ,
2022-05-04 22:12:09 +08:00
{ Action : dashboards . ActionDashboardsRead , Scope : dashboards . ScopeFoldersAll } ,
2022-03-03 22:05:47 +08:00
} ,
} ,
Grants : [ ] string { "Admin" } ,
}
2024-02-16 00:13:14 +08:00
// Needed to be able to list permissions on the general folder for viewers, doesn't actually grant access to any resources
generalFolderReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:folders.general:reader" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Reader (root)" ,
2024-02-16 00:13:14 +08:00
Description : "Access the general (root) folder." ,
Group : "Folders" ,
Hidden : true ,
Permissions : [ ] ac . Permission {
{ Action : dashboards . ActionFoldersRead , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
} ,
} ,
Grants : [ ] string { string ( org . RoleViewer ) } ,
}
2022-03-03 22:05:47 +08:00
foldersWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:folders:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2022-03-03 22:05:47 +08:00
Description : "Create, read, write or delete all folders and dashboards and their permissions." ,
Group : "Folders" ,
Permissions : ac . ConcatPermissions (
foldersReaderRole . Role . Permissions ,
[ ] ac . Permission {
2024-08-01 23:20:38 +08:00
{ Action : dashboards . ActionFoldersCreate , Scope : dashboards . ScopeFoldersAll } ,
2022-03-10 00:57:50 +08:00
{ Action : dashboards . ActionFoldersWrite , Scope : dashboards . ScopeFoldersAll } ,
{ Action : dashboards . ActionFoldersDelete , Scope : dashboards . ScopeFoldersAll } ,
2022-05-04 22:12:09 +08:00
{ Action : dashboards . ActionDashboardsWrite , Scope : dashboards . ScopeFoldersAll } ,
{ Action : dashboards . ActionDashboardsDelete , Scope : dashboards . ScopeFoldersAll } ,
{ Action : dashboards . ActionDashboardsCreate , Scope : dashboards . ScopeFoldersAll } ,
{ Action : dashboards . ActionDashboardsPermissionsRead , Scope : dashboards . ScopeFoldersAll } ,
{ Action : dashboards . ActionDashboardsPermissionsWrite , Scope : dashboards . ScopeFoldersAll } ,
2022-03-03 22:05:47 +08:00
} ) ,
} ,
Grants : [ ] string { "Admin" } ,
}
2023-10-12 07:30:50 +08:00
libraryPanelsCreatorRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:library.panels:creator" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Creator" ,
2024-08-01 23:20:38 +08:00
Description : "Create library panel under the root folder." ,
2023-10-12 07:30:50 +08:00
Group : "Library panels" ,
Permissions : [ ] ac . Permission {
{ Action : dashboards . ActionFoldersRead , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
{ Action : libraryelements . ActionLibraryPanelsCreate , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
} ,
} ,
Grants : [ ] string { "Editor" } ,
}
libraryPanelsReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:library.panels:reader" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2023-10-12 07:30:50 +08:00
Description : "Read all library panels." ,
Group : "Library panels" ,
Permissions : [ ] ac . Permission {
2023-10-26 01:44:55 +08:00
{ Action : libraryelements . ActionLibraryPanelsRead , Scope : dashboards . ScopeFoldersAll } ,
2023-10-12 07:30:50 +08:00
} ,
} ,
Grants : [ ] string { "Admin" } ,
}
libraryPanelsGeneralReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:library.panels:general.reader" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Reader (root)" ,
2024-08-01 23:20:38 +08:00
Description : "Read all library panels under the root folder." ,
2023-10-12 07:30:50 +08:00
Group : "Library panels" ,
Permissions : [ ] ac . Permission {
{ Action : libraryelements . ActionLibraryPanelsRead , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
} ,
} ,
Grants : [ ] string { "Viewer" } ,
}
libraryPanelsWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:library.panels:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2023-10-12 07:30:50 +08:00
Group : "Library panels" ,
Description : "Create, read, write or delete all library panels and their permissions." ,
Permissions : ac . ConcatPermissions ( libraryPanelsReaderRole . Role . Permissions , [ ] ac . Permission {
2023-10-26 01:44:55 +08:00
{ Action : libraryelements . ActionLibraryPanelsWrite , Scope : dashboards . ScopeFoldersAll } ,
{ Action : libraryelements . ActionLibraryPanelsDelete , Scope : dashboards . ScopeFoldersAll } ,
{ Action : libraryelements . ActionLibraryPanelsCreate , Scope : dashboards . ScopeFoldersAll } ,
2023-10-12 07:30:50 +08:00
} ) ,
} ,
Grants : [ ] string { "Admin" } ,
}
libraryPanelsGeneralWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:library.panels:general.writer" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Writer (root)" ,
2023-10-12 07:30:50 +08:00
Group : "Library panels" ,
2024-08-01 23:20:38 +08:00
Description : "Create, read, write or delete all library panels and their permissions under the root folder." ,
2023-10-12 07:30:50 +08:00
Permissions : ac . ConcatPermissions ( libraryPanelsGeneralReaderRole . Role . Permissions , [ ] ac . Permission {
{ Action : libraryelements . ActionLibraryPanelsWrite , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
{ Action : libraryelements . ActionLibraryPanelsDelete , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
{ Action : libraryelements . ActionLibraryPanelsCreate , Scope : dashboards . ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } ,
} ) ,
} ,
Grants : [ ] string { "Editor" } ,
}
2022-09-05 23:22:39 +08:00
publicDashboardsWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:dashboards.public:writer" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Writer (public)" ,
2022-09-05 23:22:39 +08:00
Description : "Create, write or disable a public dashboard." ,
Group : "Dashboards" ,
Permissions : [ ] ac . Permission {
2022-09-08 05:29:01 +08:00
{ Action : dashboards . ActionDashboardsPublicWrite , Scope : dashboards . ScopeDashboardsAll } ,
2022-09-05 23:22:39 +08:00
} ,
} ,
Grants : [ ] string { "Admin" } ,
}
2023-07-25 04:12:59 +08:00
featuremgmtReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:featuremgmt:reader" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Reader" ,
2023-07-25 04:12:59 +08:00
Description : "Read feature toggles" ,
Group : "Feature Management" ,
Permissions : [ ] ac . Permission {
{ Action : ac . ActionFeatureManagementRead } ,
} ,
} ,
Grants : [ ] string { "Admin" } ,
}
2023-08-09 23:32:28 +08:00
featuremgmtWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:featuremgmt:writer" ,
2024-01-18 22:20:28 +08:00
DisplayName : "Writer" ,
2023-08-09 23:32:28 +08:00
Description : "Write feature toggles" ,
Group : "Feature Management" ,
Permissions : [ ] ac . Permission {
{ Action : ac . ActionFeatureManagementWrite } ,
} ,
} ,
Grants : [ ] string { "Admin" } ,
}
2024-11-26 20:13:17 +08:00
snapshotsCreatorRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:snapshots:creator" ,
DisplayName : "Creator" ,
Description : "Create snapshots" ,
Group : "Snapshots" ,
Permissions : [ ] ac . Permission {
{ Action : dashboards . ActionSnapshotsCreate } ,
} ,
} ,
Grants : [ ] string { string ( org . RoleEditor ) } ,
}
snapshotsDeleterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:snapshots:deleter" ,
DisplayName : "Deleter" ,
Description : "Delete snapshots" ,
Group : "Snapshots" ,
Permissions : [ ] ac . Permission {
{ Action : dashboards . ActionSnapshotsDelete } ,
} ,
} ,
Grants : [ ] string { string ( org . RoleEditor ) } ,
}
snapshotsReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:snapshots:reader" ,
DisplayName : "Reader" ,
Description : "Read snapshots" ,
Group : "Snapshots" ,
Permissions : [ ] ac . Permission {
{ Action : dashboards . ActionSnapshotsRead } ,
} ,
} ,
Grants : [ ] string { string ( org . RoleViewer ) } ,
}
2023-10-12 07:30:50 +08:00
roles := [ ] ac . RoleRegistration { provisioningWriterRole , datasourcesReaderRole , builtInDatasourceReader , datasourcesWriterRole ,
2023-10-19 21:36:41 +08:00
datasourcesIdReaderRole , datasourcesCreatorRole , orgReaderRole , orgWriterRole ,
orgMaintainerRole , teamsCreatorRole , teamsWriterRole , teamsReaderRole , datasourcesExplorerRole ,
2022-03-22 01:28:39 +08:00
annotationsReaderRole , dashboardAnnotationsWriterRole , annotationsWriterRole ,
2022-03-03 22:05:47 +08:00
dashboardsCreatorRole , dashboardsReaderRole , dashboardsWriterRole ,
2025-09-12 19:59:37 +08:00
foldersCreatorRole , foldersReaderRole , generalFolderReaderRole , foldersWriterRole ,
2023-10-26 01:44:55 +08:00
publicDashboardsWriterRole , featuremgmtReaderRole , featuremgmtWriterRole , libraryPanelsCreatorRole ,
2024-11-26 20:13:17 +08:00
libraryPanelsReaderRole , libraryPanelsWriterRole , libraryPanelsGeneralReaderRole , libraryPanelsGeneralWriterRole ,
snapshotsCreatorRole , snapshotsDeleterRole , snapshotsReaderRole }
2023-10-12 07:30:50 +08:00
2023-12-01 22:50:55 +08:00
if hs . Features . IsEnabled ( context . Background ( ) , featuremgmt . FlagAnnotationPermissionUpdate ) {
allAnnotationsReaderRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:annotations.all:reader" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Reader (all)" ,
2023-12-01 22:50:55 +08:00
Description : "Read all annotations and tags" ,
Group : "Annotations" ,
Permissions : [ ] ac . Permission {
{ Action : ac . ActionAnnotationsRead , Scope : ac . ScopeAnnotationsTypeOrganization } ,
2024-01-27 01:17:29 +08:00
{ Action : ac . ActionAnnotationsRead , Scope : dashboards . ScopeFoldersAll } ,
2023-12-01 22:50:55 +08:00
} ,
} ,
Grants : [ ] string { string ( org . RoleAdmin ) } ,
}
allAnnotationsWriterRole := ac . RoleRegistration {
Role : ac . RoleDTO {
Name : "fixed:annotations.all:writer" ,
2025-04-08 22:15:03 +08:00
DisplayName : "Writer (all)" ,
2023-12-01 22:50:55 +08:00
Description : "Update all annotations." ,
Group : "Annotations" ,
Permissions : [ ] ac . Permission {
{ Action : ac . ActionAnnotationsCreate , Scope : ac . ScopeAnnotationsTypeOrganization } ,
2024-01-27 01:17:29 +08:00
{ Action : ac . ActionAnnotationsCreate , Scope : dashboards . ScopeFoldersAll } ,
2023-12-01 22:50:55 +08:00
{ Action : ac . ActionAnnotationsDelete , Scope : ac . ScopeAnnotationsTypeOrganization } ,
2024-01-27 01:17:29 +08:00
{ Action : ac . ActionAnnotationsDelete , Scope : dashboards . ScopeFoldersAll } ,
2023-12-01 22:50:55 +08:00
{ Action : ac . ActionAnnotationsWrite , Scope : ac . ScopeAnnotationsTypeOrganization } ,
2024-01-27 01:17:29 +08:00
{ Action : ac . ActionAnnotationsWrite , Scope : dashboards . ScopeFoldersAll } ,
2023-12-01 22:50:55 +08:00
} ,
} ,
Grants : [ ] string { string ( org . RoleAdmin ) } ,
}
roles = append ( roles , allAnnotationsReaderRole , allAnnotationsWriterRole )
}
2023-10-12 07:30:50 +08:00
return hs . accesscontrolService . DeclareFixedRoles ( roles ... )
2021-08-04 20:44:37 +08:00
}
2021-09-22 19:50:21 +08:00
2022-02-18 18:27:00 +08:00
// Metadata helpers
// getAccessControlMetadata returns the accesscontrol metadata associated with a given resource
2024-10-07 18:08:16 +08:00
func getAccessControlMetadata ( c * contextmodel . ReqContext ,
2024-03-01 19:08:00 +08:00
prefix string , resourceID string ) ac . Metadata {
2022-03-22 00:58:18 +08:00
ids := map [ string ] bool { resourceID : true }
2024-10-07 18:08:16 +08:00
return getMultiAccessControlMetadata ( c , prefix , ids ) [ resourceID ]
2022-02-18 18:27:00 +08:00
}
// getMultiAccessControlMetadata returns the accesscontrol metadata associated with a given set of resources
2022-03-24 15:58:10 +08:00
// Context must contain permissions in the given org (see LoadPermissionsMiddleware or AuthorizeInOrgMiddleware)
2024-10-07 18:08:16 +08:00
func getMultiAccessControlMetadata ( c * contextmodel . ReqContext ,
2023-08-18 18:42:18 +08:00
prefix string , resourceIDs map [ string ] bool ) map [ string ] ac . Metadata {
2023-05-31 16:58:57 +08:00
if ! c . QueryBool ( "accesscontrol" ) {
2022-02-18 18:27:00 +08:00
return map [ string ] ac . Metadata { }
}
2025-04-10 20:42:23 +08:00
if len ( c . GetPermissions ( ) ) == 0 {
2022-02-18 18:27:00 +08:00
return map [ string ] ac . Metadata { }
}
2025-04-10 20:42:23 +08:00
return ac . GetResourcesMetadata ( c . Req . Context ( ) , c . GetPermissions ( ) , prefix , resourceIDs )
2022-02-18 18:27:00 +08:00
}