User And Access

Roles

Purpose

A role is a group of permissions that allow a user who has been given the role to carry out specific actions. For instance, the super administrator, who can be regarded as the application's owner, is permitted to carry out all tasks in the application. They can add users, remove or edit, and do other things.

Features & Demos

Default Roles

VaahCMS gave you a few default roles that it built during application setup using seeds.

RolesDescription
Super AdministratorThe application's owner is super administrator. Users with super administrator privileges have full access and control over the data and application.
AdministratorUsers who have administration roles has all the permissions access and manage the data. Somebody who has access to all the administration features within the application.
ManagerUsers who have manage roles can assign a role to user.
EditorSomebody who can publish and manage posts from the CMS section including the posts of other users.
ContributorSomebody who can write and manage their own posts but cannot publish them.
RegisteredUsers who have registered roles can access only public pages.
GuestUsers who have guest roles can access only public pages.

Create New Role

You can view the video attached below to learn how to create a new role.

Assign Permission to Roles

You can view the video attached below to learn how to add permission to a role.

Assign Roles to User

You can view the video attached below to learn how to give a role to a user.

Permissions

The following permissions are necessary for roles management in order to carry out certain actions.

PermissionsDescription
Can Manage RolesThis permission enables roles to manage other roles. The user can activate, deactivate, update or delete a role with this access.
Can Delete RolesThis permission grants roles access to delete a role.
Can Update RolesThis permission grants roles to update details of a role.
Can Read RolesThis permission grants roles to read all listed roles of the application.
Can Create RolesThis permission grant roles to create a new role for the application.
Has Access Of Roles SectionThis permission grant roles to access of the roles section in the side nav bar of the application.

Files

  • Laravel Route: vaahcms/Routes/backend/route-roles.php
  • Laravel Controller: vaahcms/Http/Controllers/Backend/RolesController.php
  • Laravel Model: vaahcms/Models/Role.php
  • Vue Route: vaahcms/Vue/vaahtwo/routes/vue-routes-roles.js
  • Vue Store: vaahcms/Vue/vaahtwo/stores/store-roles.js
  • Vue Page Directory: vaahcms/Vue/vaahtwo/pages/roles

Methods

Some reusable methods mention bellowed.

Role::getActiveRoles()

You can use this method for fetching all active roles of the application.

use WebReinvent\VaahCms\Models\Role;  // Import Role class at the top

$active_roles = Role::getActiveRoles();

Role::countUsers($id)

You can use this method for count users of a role.

use WebReinvent\VaahCms\Models\Role;  // Import Role class at the top

$count_users = Role::countUsers($id);  // Role ID must be provided as parameters.

Role::countPermissions($id)

You can use this method for count permissions of a role.

use WebReinvent\VaahCms\Models\Role;  // Import Role class at the top

$count_permissions = Role::countPermissions($id);  // Role ID must be provided as parameters.

Role::syncRolesWithUsers()

You can use this method for sync roles with users. After creating a new role you have to call this method to sync role count with users.

use WebReinvent\VaahCms\Models\Role;  // Import Role class at the top

Role::syncRolesWithUsers();

VaahCMS provide a method to check the user's role.

if (Auth::user()->hasRole('administrator')) {
    //write your logic here
}

API

VaahCMS has APIs for every method, allowing you to interact with NuxJS or other frameworks.

You can access APIs method from vaachms/Http/Controllers/Api/RolesController.php and routes from vaahcms/Routes/api/api-routes-roles.php

We mention some methods bellow which help you to understand the structure.

Create Role

Method: POST
Action: creatItem
URL: <public-url>/api/role/roles/
Sample Request
parameter = [
    'name',           //required
    'slug',           //required
    'detalis',        //required
    'is_active'       //required
];
Sample Response
{
  "data": {
    "item": {
      .............
      .............
    }
  },
  "messages": [
    "Saved successfully."
  ],
  "success": "true"
}

Fetch Roles

Method: GET
Action: getList
URL: <public-url>/api/role/roles/
Sample Request
parameter = [
    'filter' => [
        'q'            // for search queary (optionl)
        'trashed'      // for include or exclude trashed data (optionl)
        'is_active'    // for fetching only active or inactive data (optionl)
        'sort'         // for sorting the data (optionl)
    ];  
];
Sample Response
{
  "data": {
      .............
      .............
  },
  "success": "true",
  "totalPermissions" : "// count of total permissions of the application ",
  "totalUsers" : "// count of total users of the application "
}

Fetch single role

Method: GET
Action: getItem($id)
URL: <public-url>/api/role/roles/{id}
Sample Response
{
  "data": {
      .............
      .............
  },
  "success": "true"
}

Delete Role

Method: DELETE
Action: delete
URL: <public-url>/api/role/roles/{id}
Sample Response
{
  "data": {},
  "message" : [
    "vaahcms-general.action_successful"
  ],
  "success": "true"
}

Create a Roles via VaahSeeder

User can create a role through seeds. Create a json file of name roles.json at .../VaahCms/Modules/{module_name}/Database/Seeds/json/ this directory.
roles.json

[

    {
        "name": "Clients",
        "details": "Can login to backend dashboard",
        "count_users": 0,
        "count_permissions":0      
    },
    {
        "name": "Customers",
        "details": "Frontend user to manage account",
        "count_users": 0,
        "count_permissions":0   
    }
]

To run this seed, you have to add some code in DatabaseTableSeeder.php at .../VaahCms/Modules/{module_name}>/Database/Seeds/ this directory.
DatabaseTableSeeder.php

<?php
namespace VaahCms\Modules\{module_name}\Database\Seeds;


use Illuminate\Database\Seeder;
use WebReinvent\VaahCms\Libraries\VaahSeeder;

class DatabaseTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        VaahSeeder::roles(__DIR__.'/json/roles.json');
    }

}
  • For run seeder Deactivate and Active your module.
  • Visit following url to see new added roles. <public-url>/backend#/vaah/roles

Copyright © 2024