Exemplo n.º 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     if (Gate::denies('user.role.create')) {
         return Redirect::back();
     } else {
         $input = $request->all();
         $id = Role::create($input)->id;
         DB::table('permission_role')->where('role_id', '=', $id)->delete();
         foreach ($input['permission_id'] as $permissionid) {
             $permissionrole = new PermissionRole();
             $permissionrole->permission_id = $permissionid;
             $permissionrole->role_id = $id;
             $permissionrole->save();
         }
         return redirect('/role');
     }
 }
Exemplo n.º 2
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();
 }
Exemplo n.º 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();
         }
     }
 }
Exemplo n.º 4
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.'));
 }
Exemplo n.º 5
0
 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.'));
     }
 }
Exemplo n.º 6
0
 public static function getFeaturePermissions($role_id)
 {
     //
     $result = [];
     $features = Role::find($role_id)->features()->wherePivot('deleted_at', null)->get();
     foreach ($features as $feature) {
         $permissions = [];
         //$permissions = Role::find($role_id)->permissions()->where('feature_id', $feature->id)->get();
         $feature_permissions = Permission::where('feature_id', $feature->id)->get();
         foreach ($feature_permissions as $fp) {
             //get the role for this feature, check whether is ther any pivot exist.
             //$roles = $fp->roles()->where('role_id', $role_id)->get(); //not using the pivot...
             $pivot = PermissionRole::whereNull('deleted_at')->where('role_id', $role_id)->where('permission_id', $fp->id)->first();
             $checked = count($pivot) > 0 ? true : false;
             $permissions[] = ['id' => $fp->id, 'feature_id' => $fp->feature_id, 'name' => $fp->name, 'descr' => $fp->descr, 'module' => $fp->module, 'position' => $fp->position, 'url' => $fp->url, 'icon' => $fp->icon, 'icon_bg' => $fp->icon_bg, 'prompt_type' => $fp->prompt_type, 'prompt_title' => $fp->prompt_title, 'prompt_content' => $fp->prompt_content, 'checked' => $checked];
         }
         $result[] = ['feature' => $feature->toArray(), 'permissions' => $permissions];
     }
     // get result ...
     return $result;
 }