diff --git a/infrastructure/perms_connectors/keto_connector.go b/infrastructure/perms_connectors/keto_connector.go index 5039b8f..9cb4051 100644 --- a/infrastructure/perms_connectors/keto_connector.go +++ b/infrastructure/perms_connectors/keto_connector.go @@ -79,22 +79,21 @@ func (k KetoConnector) CheckPermission(perm Permission, permDependancies *Permis return len(perms) > 0 } -func (k KetoConnector) DeleteRole(roleID string) (string, int, error) { - k.deleteRelationShip("", "member", roleID, nil) - _, code, err := k.deleteRelationShip(roleID, "is", k.scope(), nil) +func (k KetoConnector) deletes(object string, relation string, subject string, relation2 string) (string, int, error) { + k.deleteRelationShip(object, relation, subject, nil) + _, code, err := k.deleteRelationShip(subject, relation2, k.scope(), nil) if err != nil { return "", code, err } - return roleID, 200, nil + return subject, 200, nil +} + +func (k KetoConnector) DeleteRole(roleID string) (string, int, error) { + return k.deletes("", "member", roleID, "is") } func (k KetoConnector) DeleteGroup(groupID string) (string, int, error) { - k.deleteRelationShip("", "groups", groupID, nil) - _, code, err := k.deleteRelationShip(groupID, "groupin", k.scope(), nil) - if err != nil { - return "", code, err - } - return groupID, 200, nil + return k.deletes("", "groups", groupID, "groupin") } func (k KetoConnector) DeletePermission(permID string, relation string, internal bool) (string, int, error) { @@ -105,28 +104,15 @@ func (k KetoConnector) DeletePermission(permID string, relation string, internal } return "", 200, err } - k.deleteRelationShip("", "", permID, nil) - _, code, err := k.deleteRelationShip(permID, "permits"+meth.String(), k.scope(), nil) - if err != nil { - return "", code, err - } - return permID, 200, nil + return k.deletes("", "groups", permID, "permits"+meth.String()) } func (k KetoConnector) CreateRole(roleID string) (string, int, error) { - p, code, err := k.createRelationShip(roleID, "is", k.scope(), nil) - if err != nil { - return "", code, err - } - return p.Object, 200, nil + return k.creates(roleID, "is", k.scope()) } func (k KetoConnector) CreateGroup(groupID string) (string, int, error) { - p, code, err := k.createRelationShip(groupID, "groupin", k.scope(), nil) - if err != nil { - return "", code, err - } - return p.Object, 200, nil + return k.creates(groupID, "groupin", k.scope()) } func (k KetoConnector) CreatePermission(permID string, relation string, internal bool) (string, int, error) { @@ -134,9 +120,12 @@ func (k KetoConnector) CreatePermission(permID string, relation string, internal if err != nil { return "", 422, err } - k.BindPermission("admin", permID, "permits"+meth.String()) - p, code, err := k.createRelationShip(permID, "permits"+meth.String(), k.scope(), nil) + return k.creates(permID, "permits"+meth.String(), k.scope()) +} + +func (k KetoConnector) creates(object string, relation string, subject string) (string, int, error) { + p, code, err := k.createRelationShip(object, relation, subject, nil) if err != nil { return "", code, err } @@ -144,49 +133,29 @@ func (k KetoConnector) CreatePermission(permID string, relation string, internal } func (k KetoConnector) GetRole(roleID string) ([]string, error) { - arr := []string{} - roles, err := k.get(roleID, "is", k.scope()) - if err != nil { - return arr, err - } - for _, role := range roles { - arr = append(arr, role.Object) - } - return arr, nil + return k.gets(roleID, "is", k.scope()) } -func (k KetoConnector) GetGroup(roleID string) ([]string, error) { - arr := []string{} - groups, err := k.get(roleID, "groupin", k.scope()) - if err != nil { - return arr, err - } - for _, grp := range groups { - arr = append(arr, grp.Object) - } - return arr, nil +func (k KetoConnector) GetGroup(groupID string) ([]string, error) { + return k.gets(groupID, "groupin", k.scope()) } func (k KetoConnector) GetRoleByUser(userID string) ([]string, error) { - arr := []string{} - roles, err := k.get("", "member", userID) - if err != nil { - return arr, err - } - for _, role := range roles { - arr = append(arr, role.Object) - } - return arr, nil + return k.gets("", "member", userID) } func (k KetoConnector) GetGroupByUser(userID string) ([]string, error) { + return k.gets("", "groups", userID) +} + +func (k KetoConnector) gets(object string, relation string, subject string) ([]string, error) { arr := []string{} - groups, err := k.get("", "groups", userID) + objs, err := k.get(object, relation, subject) if err != nil { return arr, err } - for _, grp := range groups { - arr = append(arr, grp.Object) + for _, obj := range objs { + arr = append(arr, obj.Object) } return arr, nil } @@ -266,20 +235,20 @@ func (k KetoConnector) get(object string, relation string, subject string) ([]Pe return t, nil } -func (k KetoConnector) BindRole(userID string, roleID string) (string, int, error) { - _, code, err := k.createRelationShip(roleID, "member", userID, nil) +func (k KetoConnector) binds(subject string, relation string, object string) (string, int, error) { + _, code, err := k.createRelationShip(object, relation, subject, nil) if err != nil { - return roleID, code, err + return object, code, err } - return roleID, 200, nil + return object, 200, nil +} + +func (k KetoConnector) BindRole(userID string, roleID string) (string, int, error) { + return k.binds(userID, "member", roleID) } func (k KetoConnector) BindGroup(userID string, groupID string) (string, int, error) { - _, code, err := k.createRelationShip(groupID, "groups", userID, nil) - if err != nil { - return groupID, code, err - } - return groupID, 200, nil + return k.binds(userID, "groups", groupID) } func (k KetoConnector) BindPermission(roleID string, permID string, relation string) (*Permission, int, error) { @@ -308,20 +277,20 @@ func (k KetoConnector) BindPermission(roleID string, permID string, relation str }, 200, nil } -func (k KetoConnector) UnBindRole(userID string, roleID string) (string, int, error) { - _, code, err := k.deleteRelationShip(roleID, "member", userID, nil) +func (k KetoConnector) unbinds(subject string, relation string, object string) (string, int, error) { + _, code, err := k.deleteRelationShip(object, relation, subject, nil) if err != nil { - return roleID, code, err + return object, code, err } - return roleID, 200, nil + return object, 200, nil +} + +func (k KetoConnector) UnBindRole(userID string, roleID string) (string, int, error) { + return k.unbinds(userID, "member", roleID) } func (k KetoConnector) UnBindGroup(userID string, groupID string) (string, int, error) { - _, code, err := k.deleteRelationShip(groupID, "groups", userID, nil) - if err != nil { - return groupID, code, err - } - return groupID, 200, nil + return k.unbinds(userID, "groups", groupID) } func (k KetoConnector) UnBindPermission(roleID string, permID string, relation string) (*Permission, int, error) {