diff --git a/pkg/services/accesscontrol/pluginutils/utils.go b/pkg/services/accesscontrol/pluginutils/utils.go index 11890217748..362e927afb9 100644 --- a/pkg/services/accesscontrol/pluginutils/utils.go +++ b/pkg/services/accesscontrol/pluginutils/utils.go @@ -60,6 +60,22 @@ func ToRegistrations(pluginID, pluginName string, regs []plugins.RoleRegistratio return res } +// PluginIDFromName extracts the plugin ID from the role name +func PluginIDFromName(roleName string) string { + if !strings.HasPrefix(roleName, ac.PluginRolePrefix) { + return "" + } + + pluginID := strings.Builder{} + for _, c := range roleName[len(ac.PluginRolePrefix):] { + if c == ':' { + break + } + pluginID.WriteRune(c) + } + return pluginID.String() +} + func roleName(pluginID, roleName string) string { return fmt.Sprintf("%v%v:%v", ac.PluginRolePrefix, pluginID, strings.Replace(strings.ToLower(roleName), " ", "-", -1)) } diff --git a/pkg/services/sqlstore/migrations/accesscontrol/seed_assignment.go b/pkg/services/sqlstore/migrations/accesscontrol/seed_assignment.go index 7f6ce47e8e4..efbee777aa4 100644 --- a/pkg/services/sqlstore/migrations/accesscontrol/seed_assignment.go +++ b/pkg/services/sqlstore/migrations/accesscontrol/seed_assignment.go @@ -41,6 +41,12 @@ func AddSeedAssignmentMigrations(mg *migrator.Migrator) { &migrator.Index{Cols: []string{"builtin_role", "action", "scope"}, Type: migrator.UniqueIndex})) mg.AddMigration("add primary key to seed_assigment", &seedAssignmentPrimaryKeyMigrator{}) + + mg.AddMigration("add origin column to seed_assignment", + migrator.NewAddColumnMigration(seedAssignmentTable, + &migrator.Column{Name: "origin", Type: migrator.DB_Varchar, Length: 190, Nullable: true})) + + mg.AddMigration("add origin to plugin seed_assignment", &seedAssignmentOnCallMigrator{}) } type seedAssignmentPrimaryKeyMigrator struct { @@ -119,3 +125,21 @@ func (m *seedAssignmentPrimaryKeyMigrator) Exec(sess *xorm.Session, mig *migrato return nil } + +type seedAssignmentOnCallMigrator struct { + migrator.MigrationBase +} + +func (m *seedAssignmentOnCallMigrator) SQL(dialect migrator.Dialect) string { + return CodeMigrationSQL +} + +func (m *seedAssignmentOnCallMigrator) Exec(sess *xorm.Session, mig *migrator.Migrator) error { + _, err := sess.Exec( + `UPDATE seed_assignment SET origin = ? WHERE action LIKE ? OR scope = ?`, + "grafana-oncall-app", + "grafana-oncall-app%", + "plugins:id:grafana-oncall-app", + ) + return err +}