Exemplo n.º 1
0
 public function PUT_updateUser(Request $request, $id)
 {
     $theme = Theme::uses('notebook')->layout('default');
     $theme->setMenu('user.user');
     $validator = Validator::make($request->all(), ['email' => 'required|email', 'password' => 'required_with:password|min:6', 'name' => 'required', 'groups' => 'required|array']);
     $errors = array();
     if ($validator->fails()) {
         foreach ($validator->errors()->all() as $message) {
             $errors[] = $message;
         }
     }
     if (!count($errors)) {
         $chk = User::where('email', $request->input('email'))->where('id', '!=', $id)->count();
         if ($chk) {
             $errors[] = 'User with this email already exists.';
         }
     }
     if (!count($errors)) {
         $user = User::find($id);
         $user->email = $request->input('email');
         $user->name = $request->input('name');
         if ($request->has('password')) {
             $user->password = bcrypt($request->input('password'));
         }
         $user->save();
         $ug = UserGroup::where('user_id', $id);
         $ug->delete();
         $groups = $request->input('groups');
         if (is_array($groups)) {
             foreach ($groups as $key => $group) {
                 $adminGroup = Group::find($group);
                 $usergroup = UserGroup::create(array('user_id' => $user->id, 'group_id' => $adminGroup->id));
             }
         }
         return redirect(route('user.list'))->with('STATUS_OK', 'User `' . $request->input('email') . '` successfully updated.');
     } else {
         $msg = Helper::arrayToList($errors);
         return redirect(route('user.update', $id))->with('STATUS_FAIL', $msg)->withInput();
     }
 }
Exemplo n.º 2
0
 public function POST_createGroup(Request $request)
 {
     $validator = Validator::make($request->all(), ['group_name' => 'required', 'routes' => 'required|array']);
     $errors = array();
     if ($validator->fails()) {
         foreach ($validator->errors()->all() as $message) {
             $errors[] = $message;
         }
     }
     if (!count($errors)) {
         $chk = Group::where('name', $request->input('group_name'))->count();
         if ($chk) {
             $errors[] = 'Group `' . $request->input('group_name') . '` already exists.';
         }
     }
     if (!count($errors)) {
         $ar_grps = $request->input('routes');
         $ar_groups = array();
         if (is_array($ar_grps)) {
             foreach ($ar_grps as $key => $grp) {
                 $ar_groups[$grp] = 1;
             }
         }
         // $group = Group::create(array(
         $group = new Group();
         $group->name = $request->input('group_name');
         $group->permissions = json_encode($ar_groups);
         $group->save();
         return redirect(route('group.assign'))->with('STATUS_OK', 'Group `' . $request->input('group_name') . '` successfully created.');
     } else {
         $msg = Helper::arrayToList($errors);
         return redirect(route('group.create'))->with('STATUS_FAIL', $msg)->withInput();
     }
 }