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…
