Exemple #1
0
 public function role_edit(Request $request)
 {
     $this->validate($request, array('role' => 'required'));
     $permissions = array_filter(explode(',', $request->input('permissions')));
     $error_msg_role = 'The role has already existed!';
     if (!$request->input('id')) {
         if (Role::where('role', $request->input('role'))->first()) {
             return redirect()->back()->withErrors($error_msg_role);
         }
         $lastInsert = Role::create($request->all());
         $insertRoleId = $lastInsert->id;
     } else {
         $self = Role::find($request->input('id'));
         if ($self->role != $request->input('role') && Role::where('role', $request->input('role'))->first()) {
             return redirect()->back()->withErrors($error_msg_role);
         }
         $insertRoleId = $request->input('id');
         Role::find($insertRoleId)->update($request->all());
         PermissionRole::where('role_id', $insertRoleId)->delete();
     }
     foreach ($permissions as $one) {
         PermissionRole::create(array('permission_id' => $one, 'role_id' => $insertRoleId));
     }
     return redirect()->back();
 }
Exemple #2
0
 public function assignRolePermission($rid)
 {
     $inputs = Input::all();
     //get all the permission that this role has.
     $current_permissions_id = Role::getPermissionsAssignedToCurrentRole($rid);
     foreach ($inputs as $key => $value) {
         $is_permission_added_in_current_role = false;
         if ($key == '_token') {
             continue;
         }
         $permission_id = explode("_", $key)[1];
         $is_permission_added_in_current_role = in_array($permission_id, $current_permissions_id);
         if (!$is_permission_added_in_current_role && $value == 'on') {
             echo 'ON';
             print_r($current_permissions_id);
             echo $permission_id . '->';
             echo $is_permission_added_in_current_role;
             echo '<br>';
             //if the permission is not exist, create new permission role record.
             $new_permission = new PermissionRole();
             $new_permission->role_id = $rid;
             $new_permission->permission_id = $permission_id;
             $new_permission->order = '1';
             $new_permission->save();
             //else since it is already exist, and it is on, no need to do anything.
         } else {
             if ($is_permission_added_in_current_role) {
                 if ($value == 'off') {
                     echo 'OFF';
                     print_r($current_permissions_id);
                     echo $permission_id . '->';
                     echo $is_permission_added_in_current_role;
                     echo '<br>';
                     //if permission record is exist (true), find and do a delete.
                     $perm = PermissionRole::where('role_id', $rid)->where('permission_id', $permission_id)->first();
                     if (isset($perm)) {
                         $perm->delete();
                     }
                     //else it is not exist, do nothing.
                 } else {
                     if ($value == 'on') {
                         //echo 'ON'; print_r ($current_permissions_id); echo $permission_id.'->'; echo $is_permission_added_in_current_role; echo '<br>';
                         //do nothing ....
                     }
                 }
             }
         }
     }
     //return '';
     return Redirect::to('/crm/role/' . $rid)->withMessage(Generate::message('Success', 'New Permissions Applied, please relogin to see the changes.'));
 }
Exemple #3
0
 /**
  * Update the specified resource in storage.
  *
  * @param $role
  * @return Response
  */
 public function postEdit(RoleRequest $request, $id)
 {
     $is_admin = 0;
     if (!empty($request->permission)) {
         $permissionsAdmin = Permission::where('is_admin', '=', 1)->get();
         foreach ($permissionsAdmin as $perm) {
             foreach ($request->permission as $item) {
                 if ($item == $perm['id'] && $perm['is_admin'] == '1') {
                     $is_admin = 1;
                 }
             }
         }
     }
     $role = Role::find($id);
     $role->is_admin = $is_admin;
     $role->name = $request->name;
     $role->save();
     PermissionRole::where('role_id', '=', $id)->delete();
     if (is_array($request->permission)) {
         foreach ($request->permission as $item) {
             $permission = new PermissionRole();
             $permission->permission_id = $item;
             $permission->role_id = $role->id;
             $permission->save();
         }
     }
 }
 public function assignRolePermission($role_id)
 {
     try {
         $permissions = Input::get('permissions');
         $inputs = Input::all();
         $permission_ids = PermissionRole::where('role_id', $role_id)->lists('permission_id')->toArray();
         foreach ($permissions as $key => $permission_id) {
             if (in_array($permission_id, $permission_ids) && $inputs["permission_status_{$permission_id}"] == 'off') {
                 //Turn off permission.
                 PermissionRole::where('role_id', $role_id)->where('permission_id', $permission_id)->delete();
             } elseif (!in_array($permission_id, $permission_ids) && $inputs["permission_status_{$permission_id}"] == 'on') {
                 $new_assign_permission = ['role_id' => $role_id, 'permission_id' => $permission_id, 'order' => 1];
                 PermissionRole::create($new_assign_permission);
             }
         }
         return Redirect::to("/crm/role/{$role_id}")->withMessage(Generate::success_message('Success', 'Assigned Successfully'));
     } catch (Exception $e) {
         return Redirect::to("/crm/role/{$role_id}")->withMessage(Generate::error_message('Fail', 'Failed to assign permission.'));
     }
 }