Skip to content

Commit 33da037

Browse files
authored
refactor: refactoring AuthorizationBackend (#287)
1 parent 2afe739 commit 33da037

6 files changed

Lines changed: 8 additions & 4 deletions

File tree

core/security/role_registry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func GetAllPermissionsForUser(user *UserSessionInfo) []PermissionKey {
181181
}
182182

183183
func getPermissionKeysByUser(user *UserSessionInfo) ([]PermissionKey, error) {
184+
184185
ctx1 := context.Background()
185186
if val, ok := user.GetStringArray(orm.TeamsIDKey); ok {
186187
ctx1 = context.WithValue(ctx1, orm.TeamsIDKey, val)
@@ -191,7 +192,7 @@ func getPermissionKeysByUser(user *UserSessionInfo) ([]PermissionKey, error) {
191192
p, ok := value.(AuthorizationBackend)
192193
if ok {
193194
hit = true
194-
v := p.GetPermissionKeysByUserID(ctx1, user.UserID)
195+
v := p.GetPermissionKeysByUserID(ctx1, user.Provider, user.UserID)
195196
out = append(out, v...)
196197
}
197198
return true

core/security/service_registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type AuthenticationBackend interface {
1717
}
1818

1919
type AuthorizationBackend interface {
20-
GetPermissionKeysByUserID(ctx context.Context, userID string) []PermissionKey
20+
GetPermissionKeysByUserID(ctx context.Context, providerID, userID string) []PermissionKey
2121
GetPermissionKeysByRoles(ctx context.Context, roles []string) []PermissionKey
2222
}
2323

modules/security/account/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func init() {
15-
api.HandleUIMethod(api.GET, "/account/profile", Profile, api.OptionLogin(), api.AllowOPTIONSS(), api.Feature(api.FeatureCORS))
15+
api.HandleUIMethod(api.GET, "/account/profile", Profile, api.RequireLogin(), api.AllowOPTIONSS(), api.Feature(api.FeatureCORS))
1616
}
1717

1818
func Profile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

modules/security/oauth_client/provider/github/profile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func safeDereference(strPtr *string) string {
5353
}
5454

5555
func (handler *ProfileAPI) GetProfile(ctx *orm.Context, appConfig *config.OAuthConfig, cfg *oauth2.Config, tkn *oauth2.Token) *security.UserExternalProfile {
56+
5657
//get user info
5758
client := github.NewClient(cfg.Client(oauth2.NoContext, tkn))
5859
user, res, err := client.Users.Get(oauth2.NoContext, "")
@@ -62,6 +63,7 @@ func (handler *ProfileAPI) GetProfile(ctx *orm.Context, appConfig *config.OAuthC
6263
profile := security.UserExternalProfile{}
6364
login := safeDereference(user.Login)
6465
profile.ID = provider.GetExternalUserProfileID("github", login)
66+
profile.SetOwnerID(login)
6567
profile.AuthProvider = "github"
6668
profile.Login = login
6769
profile.Email = safeDereference(user.Email)

modules/security/oauth_client/provider/google/profile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (handler *ProfileAPI) GetProfile(ctx *orm.Context, appConfig *config.OAuthC
7575

7676
profile := security.UserExternalProfile{}
7777
profile.ID = provider.GetExternalUserProfileID("google", userInfo.Sub)
78+
profile.SetOwnerID(userInfo.Sub)
7879
profile.AuthProvider = "google"
7980
profile.Login = userInfo.Sub
8081
profile.Email = userInfo.Email

modules/security/rbac/role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func CreateRole(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
194194
type SecurityBackendProvider struct {
195195
}
196196

197-
func (provider *SecurityBackendProvider) GetPermissionKeysByUserID(ctx1 context.Context, userID string) []security.PermissionKey {
197+
func (provider *SecurityBackendProvider) GetPermissionKeysByUserID(ctx1 context.Context, providerID, userID string) []security.PermissionKey {
198198
var allowedPermissions = []security.PermissionKey{}
199199

200200
//bypass managed mode

0 commit comments

Comments
 (0)