public function run()
 {
     DB::table('users')->delete();
     DB::table('groups')->delete();
     DB::table('users_groups')->delete();
     $user = new User();
     $user->email = '*****@*****.**';
     $user->password = \Hash::make("admin");
     $user->first_name = 'System';
     $user->last_name = 'Admin';
     $user->activated = 1;
     $user->save();
     $admin_group = new Group();
     $admin_group->name = 'Admin';
     $admin_group->permissions = json_encode(array('admin.view' => 1, 'admin.create' => 1, 'admin.delete' => 1, 'admin.update' => 1));
     $admin_group->save();
     $user_group = new Group();
     $user_group->name = 'User';
     $user_group->permissions = json_encode(array('admin.view' => 0, 'admin.create' => 0, 'admin.delete' => 0, 'admin.update' => 0));
     $user_group->save();
     // Assign user permissions
     $user->groups()->save($admin_group);
 }
 public function postStore()
 {
     $sid = Input::get('id');
     $errors = new MessageBag();
     $rules = array('first_name' => 'required', 'last_name' => 'required', 'role' => 'required', 'email' => 'required|unique:users,email,' . $sid, 'permission-inherit' => 'regex:/^[a-z,0-9._\\-?]+$/i', 'permission-allow' => 'regex:/^[a-z,0-9._\\-?]+$/i', 'permission-deny' => 'regex:/^[a-z,0-9._\\-?]+$/i');
     // Get activated input first, for checking if user is deactivating own account
     $activated = Input::get('activated') == '' ? false : true;
     if (isset($sid)) {
         $rules['password'] = '******';
         $path = 'admin/users/edit/' . $sid;
         $user = User::find($sid);
         // Check if this is logged in user, prevent deactivate
         if (Auth::user()->id == $sid && $activated == false) {
             $errors->add('deactivateError', Lang::get('redminportal::messages.user_error_deactivate_own_account'));
             return redirect($path)->withErrors($errors)->withInput();
         }
     } else {
         $rules['password'] = '******';
         $path = 'admin/users/create';
         $user = new User();
     }
     $messages = array('permission-inherit.regex' => 'The permission inherit format is invalid. Try using the Permission Builder.', 'permission-allow.regex' => 'The permission allow format is invalid. Try using the Permission Builder.', 'permission-deny.regex' => 'The permission deny format is invalid. Try using the Permission Builder.');
     $validation = Validator::make(Input::all(), $rules, $messages);
     if ($validation->fails()) {
         return redirect($path)->withErrors($validation)->withInput();
     }
     // If user can't be created or found
     if ($user == null) {
         $errors->add('createError', Lang::get('redminportal::messages.user_error_create_unknown'));
         return redirect('/admin/users')->withErrors($errors);
     }
     $permissions = $this->populatePermission(Input::get('permission-inherit'), Input::get('permission-allow'), Input::get('permission-deny'));
     // Save or Update
     $user->email = Input::get('email');
     $password = Input::get('password');
     if ($password != '') {
         $user->password = Hash::make($password);
     }
     $user->first_name = Input::get('first_name');
     $user->last_name = Input::get('last_name');
     $user->activated = $activated;
     $user->permissions = json_encode($permissions);
     if (!$user->save()) {
         $errors->add('saveError', Lang::get('redminportal::messages.user_error_update_unknown'));
         return redirect($path)->withErrors($errors)->withInput();
     }
     // Assign group(s) to user
     // Return error message if group has error
     if (!$user->addGroup(Input::get('role'))) {
         $errors->add('groupError', Lang::get('redminportal::messages.user_error_group_not_found'));
         return redirect($path)->withErrors($errors)->withInput();
     }
     return redirect('admin/users');
 }