Skip to content

Commit

Permalink
Show user roles in user profile + fix roles priority check (#1193)
Browse files Browse the repository at this point in the history
* feat: show roles in user profile

* chore feat: update i18n gettext files

* feat: fetch roles and cache them

* refactor: ApiResolver do not need rolePriority

* chore fix: phpcs

* chore feat: update gettext po files

* refactor: get it simpler

* fix: restore po files
  • Loading branch information
didoda authored Oct 22, 2024
1 parent 01e4be4 commit 56a3daa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
14 changes: 8 additions & 6 deletions resources/js/app/components/relation-view/roles-list-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import RelationshipsView from 'app/components/relation-view/relationships-view/relationships-view';
export default {
name: 'roles-list-view',
extends: RelationshipsView,
name: 'RolesListView',
props: {
groups: {
type: Object,
Expand All @@ -33,10 +33,6 @@ export default {
type: Array,
default: () => [],
},
userRolePriority: {
type: Number,
default: () => 500,
},
userRoles: {
type: Array,
default: () => [],
Expand All @@ -47,7 +43,9 @@ export default {
return {
method: 'resources',
objectsByGroups: {},
prioritiesMap: {},
removedRelations: [],
userRolePriority: 500,
}
},
Expand All @@ -68,8 +66,12 @@ export default {
for (let roleName of groups[groupName]) {
const role = this.objects.filter((v) => v.attributes.name === roleName)[0];
this.objectsByGroups[groupName].push(role);
this.prioritiesMap[role.attributes.name] = role.meta.priority;
}
}
this.userRolePriority = this.userRoles.reduce((acc, role) => {
return Math.min(acc, this.prioritiesMap[role]);
}, 500);
this.objectsByGroups = {...this.objectsByGroups};
});
},
Expand Down
13 changes: 1 addition & 12 deletions src/Identifier/Resolver/ApiResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,8 @@ public function find(array $conditions, $type = self::TYPE_AND)
}

$roles = Hash::extract($result, 'included.{n}.attributes.name');
// check if meta.priority is available (api versions >= 4.11.0|5.7.0)
$hasPriority = false;
foreach ($result['included'] as $included) {
if (Hash::check($included, 'meta.priority')) {
$hasPriority = true;
break;
}
}
$priorities = Hash::extract($result, 'included.{n}.meta.priority');
// if api do not expose meta.priority, user priority is 0 (admin)
$rolePriority = $hasPriority ? min($priorities) : 0;
$tokens = $apiClient->getTokens();

return $result['data'] + compact('tokens') + compact('roles', 'rolePriority');
return $result['data'] + compact('roles', 'tokens');
}
}
1 change: 0 additions & 1 deletion templates/Element/Form/roles.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
:related-objects="objects"
:groups="{{ rolesGroups|length == 0 ? '{}' : rolesGroups|json_encode }}"
:user-roles="{{ user.roles|default([])|json_encode }}"
:user-role-priority="{{ user.rolePriority|default(500)|json_encode }}"
@remove-relations="setRemovedRelated">
</roles-list-view>
{% do Form.unlockField('relations.' ~ relationName ~ '.addRelated') %}
Expand Down
14 changes: 14 additions & 0 deletions templates/Pages/UserProfile/view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@
</section>
{% endif %}
{% endfor %}
<section class="fieldset">
<header>
<h2>{{ __('Roles') }}</h2>
</header>
<div class="tab-container">
<div>
{% for role in user.roles %}
<span class="tag is-black mx-05">
{{ role }}
</span>
{% endfor %}
</div>
</div>
</section>
</div>

<div class="side-view-column">
Expand Down

0 comments on commit 56a3daa

Please sign in to comment.