Integrating System Roles API in Open Event Frontend
The Eventyay system supports different system roles and allows to set panel permissions for every role. The system supports two inbuilt roles namely Admin and Super Admin. The users having access to permissions panel can create new custom system roles and define set of panel permissions for them. Also the users are provided with the option of editing and deleting any system role except the two inbuilt system roles. The feature is implemented using custom-system-roles and panel-permissions API on the server.
Adding route for system-roles
The route for custom-system-system roles is defined which contains a model returning user permissions, system roles and the panel permissions. The model is defined as async so that the execution is paused while fetching the data from the store by adding the await expression.
async model() { return { userPermissions : await this.get('store').findAll('user-permission'), systemRoles : await this.get('store').findAll('custom-system-role'), panelPermissions : await this.get('store').findAll('panel-permission') }; },
The route created above gets all the data for user permissions, system-roles and panel permissions which is later used by the template for rendering of data.
Adding model for system-roles and panel-permissions
The model for system-roles is created which contains the ‘name’ attribute of type string and a relationship with panel permissions. Every system role can have multiple panel permissions, therefore a hasMany relationship is defined in the model.
export default ModelBase.extend({ name: attr('string'), panelPermissions: hasMany('panelPermission') });
Similarly, the model for panel-permissions is added to the models directory. The defined model contains ‘panelName’ as an attribute of type string and a bool value canAccess, defining if the panel is accessible by any role or not.
export default ModelBase.extend({ panelName : attr('string'), canAccess : attr('boolean') });
Defining controller for system-roles
The controller for system-roles is defined in the controllers/admin/permissions directory. The action for adding, updating and deleting system roles are defined in the controller. While adding the system roles, all the panels are fetched and checked which panel permissions are selected by the admin. A special property namely ‘isChecked’ is added to every panel permission checkbox which toggles on change. If the property is set true the corresponding panel is added to the panel permissions relationship of corresponding role. If no panel is selected, an error message to select atleast one panel is displayed.
deleteSystemRole(role) { this.set('isLoading', true); role.destroyRecord() ... // Notify success or failure }, addSystemRole() { this.set('isLoading', true); let panels = this.get('panelPermissions'); panels.forEach(panel => { if (panel.isChecked) { this.get('role.panelPermissions').addObject(panel); } else { this.get('role.panelPermissions').removeObject(panel); } }); if (!this.get('role.panelPermissions').length) { // Notification to select atleast one panel } else { this.get('role').save() // Notify success or failure } }, updatePermissions() { this.set('isLoading', true); this.get('model.userPermissions').save() ... // Notify success or failure }
The actions defined above in the controller can be used in template by passing the appropriate parameters if required. The addSystemRole action makes a POST request to server for creating a new system role, the updatePermissions action makes a PATCH request for updating the existing system role and the deleteSystemRole action makes a delete request to the server for deleting the role.
Adding data to template for system-roles
The data obtained from the model defined in route is rendered in the template for system-roles. A loop for showing all system roles is added to the template with the name attribute containing the name of system role and another loop is added to display the panel permissions for the corresponding role.
{{#each model.systemRoles as |role|}} <tr> <td>{{role.name}}</td> <td> <div class="ui bulleted list"> {{#each role.panelPermissions as |permission|}} <div class="item">{{concat permission.panelName ' panel'}}</div> {{/each}} </div> </td> <td> // Buttons for editing and deleting roles </td> </tr> {{/each}}
A modal is to the component for creating and editing system roles. The data from this template is passed to the modal where the existing permissions are already checked and can be modified by the admins.
Resources
- Open Event Frontend Repository: https://github.com/fossasia/open-event-frontend
- Pull Request: https://github.com/fossasia/open-event-frontend/pull/1598
- Issue: https://github.com/fossasia/open-event-frontend/issues/1597
- Additional Resources: https://guides.emberjs.com/v2.5.0/, https://guides.emberjs.com/release/tutorial/routes-and-templates/, https://guides.emberjs.com/release/models/defining-models/
You must be logged in to post a comment.