From b40e6234822e5d26b4df1fc77f2017e260b127d3 Mon Sep 17 00:00:00 2001 From: Bruno Gaspar Date: Tue, 18 Mar 2014 17:22:18 +0000 Subject: [PATCH] updates, added user management (not finished) Signed-off-by: Bruno Gaspar --- app/controllers/AuthController.php | 18 ++- app/controllers/AuthorizedController.php | 12 ++ app/controllers/GroupsController.php | 8 +- app/controllers/UsersController.php | 188 +++++++++++++++++++++++ app/routes.php | 11 +- app/views/sentry/groups/form.blade.php | 22 ++- app/views/sentry/users/form.blade.php | 56 +++++++ app/views/sentry/users/index.blade.php | 52 +++++++ app/views/template.blade.php | 6 + 9 files changed, 357 insertions(+), 16 deletions(-) create mode 100644 app/controllers/AuthorizedController.php create mode 100644 app/controllers/UsersController.php create mode 100644 app/views/sentry/users/form.blade.php create mode 100644 app/views/sentry/users/index.blade.php diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 56940536..bffd4ae6 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -24,12 +24,14 @@ public function processLogin() { try { - $rules = array( + $input = Input::all(); + + $rules = [ 'email' => 'required|email', 'password' => 'required', - ); + ]; - $validator = Validator::make(Input::get(), $rules); + $validator = Validator::make($input, $rules); if ($validator->fails()) { @@ -76,20 +78,22 @@ public function register() */ public function processRegistration() { - $rules = array( + $input = Input::all(); + + $rules = [ 'email' => 'required|email|unique:users', 'password' => 'required', 'password_confirm' => 'required|same:password', - ); + ]; - $validator = Validator::make(Input::get(), $rules); + $validator = Validator::make($input, $rules); if ($validator->fails()) { return Redirect::back()->withInput()->withErrors($validator); } - if ($user = Sentry::register(Input::get())) + if ($user = Sentry::register($input)) { $code = Activation::create($user); diff --git a/app/controllers/AuthorizedController.php b/app/controllers/AuthorizedController.php new file mode 100644 index 00000000..6b2d6eb0 --- /dev/null +++ b/app/controllers/AuthorizedController.php @@ -0,0 +1,12 @@ +beforeFilter('auth'); + + $this->user = Sentry::getUser(); + } + +} diff --git a/app/controllers/GroupsController.php b/app/controllers/GroupsController.php index 4a16ac0c..fafd4655 100644 --- a/app/controllers/GroupsController.php +++ b/app/controllers/GroupsController.php @@ -1,6 +1,6 @@ groups = Sentry::getGroupRepository()->createModel(); } @@ -126,10 +128,10 @@ protected function processForm($mode, $id = null) { $input = Input::all(); - $rules = array( + $rules = [ 'name' => 'required', 'slug' => 'required|unique:groups' - ); + ]; if ($id) { diff --git a/app/controllers/UsersController.php b/app/controllers/UsersController.php new file mode 100644 index 00000000..d76c142f --- /dev/null +++ b/app/controllers/UsersController.php @@ -0,0 +1,188 @@ +users = Sentry::getUserRepository(); + } + + /** + * Display a listing of users. + * + * @return \Illuminate\View\View + */ + public function index() + { + $users = $this->users->createModel()->paginate(); + + return View::make('sentry.users.index', compact('users')); + } + + /** + * Show the form for creating new user. + * + * @return \Illuminate\View\View + */ + public function create() + { + return $this->showForm('create'); + } + + /** + * Handle posting of the form for creating new user. + * + * @return \Illuminate\Http\RedirectResponse + */ + public function store() + { + return $this->processForm('create'); + } + + /** + * Show the form for updating user. + * + * @param int $id + * @return mixed + */ + public function edit($id) + { + return $this->showForm('update', $id); + } + + /** + * Handle posting of the form for updating user. + * + * @param int $id + * @return \Illuminate\Http\RedirectResponse + */ + public function update($id) + { + return $this->processForm('update', $id); + } + + /** + * Remove the specified user. + * + * @param int $id + * @return \Illuminate\Http\RedirectResponse + */ + public function delete($id) + { + if ($user = $this->users->createModel()->find($id)) + { + $user->delete(); + + return Redirect::to('users'); + } + + return Redirect::to('users'); + } + + /** + * Shows the form. + * + * @param string $mode + * @param int $id + * @return mixed + */ + protected function showForm($mode, $id = null) + { + if ($id) + { + if ( ! $user = $this->users->createModel()->find($id)) + { + return Redirect::to('users'); + } + } + else + { + $user = $this->users->createModel(); + } + + return View::make('sentry.users.form', compact('mode', 'user')); + } + + /** + * Processes the form. + * + * @param string $mode + * @param int $id + * @return \Illuminate\Http\RedirectResponse + */ + protected function processForm($mode, $id = null) + { + $input = array_filter(Input::all()); + + $rules = [ + 'first_name' => 'required', + 'last_name' => 'required', + 'email' => 'required|unique:users' + ]; + + if ($id) + { + $user = $this->users->createModel()->find($id); + + $rules['email'] .= ",email,{$user->email},email"; + + $messages = $this->validateUser($input, $rules); + + if ($messages->isEmpty()) + { + $this->users->update($user, $input); + } + } + else + { + $messages = $this->validateUser($input, $rules); + + if ($messages->isEmpty()) + { + $user = $this->users->create($input); + + $code = Activation::create($user); + + Activation::complete($user, $code); + } + } + + if ($messages->isEmpty()) + { + return Redirect::to('users'); + } + + return Redirect::back()->withInput()->withErrors($messages); + } + + /** + * Validates a user. + * + * @param array $data + * @param mixed $id + * @return \Illuminate\Support\MessageBag + */ + protected function validateUser($data, $rules) + { + $validator = Validator::make($data, $rules); + + $validator->passes(); + + return $validator->errors(); + } + +} diff --git a/app/routes.php b/app/routes.php index c53f574f..47782cf7 100644 --- a/app/routes.php +++ b/app/routes.php @@ -11,8 +11,14 @@ | */ +Route::get('logout', function() +{ + Sentry::logout(); + + return Redirect::to('/'); +}); -Route::group(array('prefix' => 'groups'), function() +Route::group(['prefix' => 'groups'], function() { Route::get('/', 'GroupsController@index'); Route::get('create', 'GroupsController@create'); @@ -22,7 +28,7 @@ Route::get('{id}/delete', 'GroupsController@delete'); }); -Route::group(array('prefix' => 'users'), function() +Route::group(['prefix' => 'users'], function() { Route::get('/', 'UsersController@index'); Route::get('create', 'UsersController@create'); @@ -170,6 +176,7 @@ })->where('id', '\d+'); + Route::group(['prefix' => 'account', 'before' => 'auth'], function() { Route::get('/', function() diff --git a/app/views/sentry/groups/form.blade.php b/app/views/sentry/groups/form.blade.php index 6c0ca915..f89f3687 100644 --- a/app/views/sentry/groups/form.blade.php +++ b/app/views/sentry/groups/form.blade.php @@ -3,16 +3,30 @@ {{-- Page content --}} @section('body') + +
-
+
+ - + + + + {{{ $errors->first('name', ':message') }}} +
-
+
+ - + + + + {{{ $errors->first('slug', ':message') }}} +
diff --git a/app/views/sentry/users/form.blade.php b/app/views/sentry/users/form.blade.php new file mode 100644 index 00000000..84ffcd05 --- /dev/null +++ b/app/views/sentry/users/form.blade.php @@ -0,0 +1,56 @@ +@extends('template') + +{{-- Page content --}} +@section('body') + + + + + +
+ + + + + + {{{ $errors->first('first_name', ':message') }}} + +
+ +
+ + + + + + {{{ $errors->first('last_name', ':message') }}} + +
+ +
+ + + + + + {{{ $errors->first('email', ':message') }}} + +
+ +
+ + + + + + {{{ $errors->first('password', ':message') }}} + +
+ + + + + +@stop diff --git a/app/views/sentry/users/index.blade.php b/app/views/sentry/users/index.blade.php new file mode 100644 index 00000000..02a77fd8 --- /dev/null +++ b/app/views/sentry/users/index.blade.php @@ -0,0 +1,52 @@ +@extends('template') + +{{-- Page content --}} +@section('body') + + + +@if ($users->count()) +Page {{ $users->getCurrentPage() }} of {{ $users->getLastPage() }} + +
+ {{ $users->links() }} +
+ +

+ + + + + + + + + @foreach ($users as $user) + + + + + + @endforeach + +
NameEmailActions
{{ $user->first_name }} {{ $user->last_name }}{{ $user->email }} + id}") }}">Edit + id}/delete") }}">Delete +
+ +Page {{ $users->getCurrentPage() }} of {{ $users->getLastPage() }} + +
+ {{ $users->links() }} +
+@else +
+ + Nothing to show here. + +
+@endif + +@stop diff --git a/app/views/template.blade.php b/app/views/template.blade.php index 1a042cb7..f67d2821 100644 --- a/app/views/template.blade.php +++ b/app/views/template.blade.php @@ -49,6 +49,12 @@ Groups @endif + @if (Sentry::check()) + + @endif