2021-04-18 23:19:16 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UserRolesHelper
|
2023-03-08 19:10:36 +08:00
|
|
|
def user_roles_collection(object, with_inherit: false)
|
|
|
|
permission_group = "#{object.class.name}Permissions".constantize
|
|
|
|
permissions = permission_group.constants.map { |const| permission_group.const_get(const) }
|
|
|
|
|
|
|
|
roles = user_roles_subset_by_permissions(permissions).order(id: :asc).pluck(:name, :id)
|
2023-01-23 05:23:18 +08:00
|
|
|
roles = [[t('access_permissions.reset'), 'reset']] + roles if with_inherit
|
|
|
|
roles
|
2021-04-18 23:19:16 +08:00
|
|
|
end
|
2022-05-19 15:04:25 +08:00
|
|
|
|
|
|
|
def team_user_roles_collection
|
|
|
|
team_permissions =
|
|
|
|
PermissionExtends::TeamPermissions.constants.map { |const| TeamPermissions.const_get(const) } +
|
|
|
|
ProtocolPermissions.constants.map { |const| ProtocolPermissions.const_get(const) } +
|
|
|
|
RepositoryPermissions.constants.map { |const| RepositoryPermissions.const_get(const) }
|
2023-03-08 19:10:36 +08:00
|
|
|
|
|
|
|
user_roles_subset_by_permissions(team_permissions)
|
|
|
|
.sort_by { |user_role| (user_role.permissions & team_permissions).length }
|
|
|
|
.reverse!
|
2022-05-19 15:04:25 +08:00
|
|
|
end
|
2022-06-01 21:06:19 +08:00
|
|
|
|
2022-10-06 23:55:51 +08:00
|
|
|
def team_user_roles_for_select
|
|
|
|
team_user_roles_collection.pluck(:name, :id)
|
|
|
|
end
|
|
|
|
|
2022-06-01 21:06:19 +08:00
|
|
|
def managing_team_user_roles_collection
|
2023-03-08 19:10:36 +08:00
|
|
|
user_roles_subset_by_permissions([TeamPermissions::USERS_MANAGE])
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_roles_subset_by_permissions(permissions)
|
|
|
|
UserRole.where('permissions && ARRAY[?]::varchar[]', permissions)
|
2022-06-01 21:06:19 +08:00
|
|
|
end
|
2021-04-18 23:19:16 +08:00
|
|
|
end
|