public function postStore() { $sid = \Input::get('id'); $rules = array('first_name' => 'required', 'last_name' => 'required', 'email' => 'required'); if (isset($sid)) { $rules['password'] = '******'; } else { $rules['password'] = '******'; } $validation = \Validator::make(\Input::all(), $rules); $path = isset($sid) ? 'admin/users/edit/' . $sid : 'admin/users/create'; if ($validation->fails()) { return redirect($path)->withErrors($validation)->withInput(); } $first_name = \Input::get('first_name'); $last_name = \Input::get('last_name'); $email = \Input::get('email'); $password = \Input::get('password'); $role = \Input::get('role'); $activated = \Input::get('activated') == '' ? false : true; $user = isset($sid) ? User::find($sid) : new User(); if ($user == null) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('editError', "The user cannot be found or created. Please try again later."); return redirect('/admin/users')->withErrors($errors); } // Save or Update $user->email = $email; if ($password != '') { $user->password = \Hash::make($password); } $user->first_name = $first_name; $user->last_name = $last_name; $user->activated = $activated; if (!$user->save()) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('editError', "The user cannot be updated due to some problem. Please try again."); return redirect($path)->withErrors($errors)->withInput(); } // Find user's group $old_group = $user->groups()->first(); $new_group = Group::find($role); if ($new_group == null) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('editError', "The user cannot be updated because the selected group cannot be found. Please try again."); return redirect($path)->withErrors($errors)->withInput(); } // Assign the group to the user if ($old_group == null) { $user->groups()->save($new_group); } elseif ($old_group->id != $new_group->id) { $user->groups()->detach(); $user->groups()->save($new_group); } return redirect('admin/users'); }
public function getDelete($sid) { $group = Group::find($sid); if ($group == null) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('deleteError', "The group cannot be deleted at this time. It may have already been deleted."); return redirect('/admin/groups')->withErrors($errors); } if (count($group->users) > 0) { // Prevent deletion of this group $errors = new \Illuminate\Support\MessageBag(); $errors->add('deleteError', "The group cannot be deleted because it is in use. Try moving the users to another group first."); return redirect('/admin/groups')->withErrors($errors); } else { $group->delete(); } return redirect('admin/groups'); }
/** /* Add Group(s) to User /* @param Group can be single Id or array of Group Id /* @return bool True if successful */ public function addGroup($group_id) { $successful = true; if ($group_id == null) { return false; } // Remove all existing group(s) from user $this->groups()->detach(); // Assign group(s) to user if (is_array($group_id)) { // If multiple roles if (count($group_id) > 0) { foreach ($group_id as $item) { $new_group = Group::find($item); if ($new_group == null) { $successful = false; } else { $this->groups()->save($new_group); } } } } else { $new_group = Group::find($group_id); if ($new_group == null) { $successful = false; } else { $this->groups()->save($new_group); } } return $successful; }