Example #1
0
 public function testMinWithConditions()
 {
     $this->assertEquals(1, User::min(array('column' => 'id', 'conditions' => 'id between 1 and 4')));
 }
 /**
  * Handles POST actions for the user module
  *
  * @return \Illuminate\Support\Facades\Redirect
  */
 public function postUser()
 {
     if (Input::has('_save')) {
         $id = Input::get('id');
         // Define validation rules
         $validator = Validator::make(Input::all(), array('username' => 'required|max:50|alpha_dash|unique:users,username,' . $id . ',id,type,db', 'email' => 'required|max:100|email|unique:users,email,' . $id . ',id,type,db', 'dispname' => 'max:100', 'password' => empty($id) ? 'required|min:5' : 'min:5'));
         // Run the validator
         if ($validator->passes()) {
             // If ID is there, it is an update operation
             if (!empty($id)) {
                 $user = User::findOrFail($id);
                 $origUsername = $user->username;
             } else {
                 $user = new User();
                 $origUsername = NULL;
             }
             $user->username = Input::get('username');
             $user->email = Input::get('email');
             $user->dispname = Input::get('dispname');
             $user->salt = $user->salt ?: str_random(5);
             // The first user is always immutable
             $isFounder = $user->id == User::min('id');
             $user->admin = $isFounder ?: Input::has('admin');
             $user->active = $isFounder ?: Input::has('active');
             if (Input::has('password')) {
                 $user->password = PHPass::make()->create(Input::get('password'), $user->salt);
             }
             $user->save();
             // Username is cached in the main, comment and revision tables, update them too
             if (!empty($id)) {
                 Paste::where('author_id', $id)->update(array('author' => $user->username));
                 Revision::where('author', $origUsername)->update(array('author' => $user->username));
                 Comment::where('author', $origUsername)->update(array('author' => $user->username));
             }
             Cache::flush();
             Session::flash('messages.success', Lang::get('admin.user_saved'));
             return Redirect::to('admin/user');
         } else {
             Session::flash('messages.error', $validator->messages()->all('<p>:message</p>'));
             return Redirect::to(URL::previous())->withInput();
         }
     } else {
         if (Input::has('search')) {
             $username = Input::get('search');
             return Redirect::to('admin/user/edit/' . urlencode($username));
         } else {
             return Redirect::to('admin/user');
         }
     }
 }