Skip to content

Commit

Permalink
updates, added user management (not finished)
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Gaspar <[email protected]>
brunogaspar committed Mar 18, 2014
1 parent 0eb6178 commit b40e623
Showing 9 changed files with 357 additions and 16 deletions.
18 changes: 11 additions & 7 deletions app/controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -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);

12 changes: 12 additions & 0 deletions app/controllers/AuthorizedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class AuthorizedController extends BaseController {

public function __construct()
{
$this->beforeFilter('auth');

$this->user = Sentry::getUser();
}

}
8 changes: 5 additions & 3 deletions app/controllers/GroupsController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class GroupsController extends AuthController {
class GroupsController extends AuthorizedController {

/**
* Holds the Sentry Groups repository.
@@ -16,6 +16,8 @@ class GroupsController extends AuthController {
*/
public function __construct()
{
parent::__construct();

$this->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)
{
188 changes: 188 additions & 0 deletions app/controllers/UsersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

class UsersController extends AuthorizedController {

/**
* Holds the Sentry Users repository.
*
* @var \Cartalyst\Sentry\Users\EloquentUser
*/
protected $users;

/**
* Constructor.
*
* @return void
*/
public function __construct()
{
parent::__construct();

$this->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();
}

}
11 changes: 9 additions & 2 deletions app/routes.php
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 18 additions & 4 deletions app/views/sentry/groups/form.blade.php
Original file line number Diff line number Diff line change
@@ -3,16 +3,30 @@
{{-- Page content --}}
@section('body')

<div class="page-header">
<h1>{{ $mode == 'create' ? 'Create Group' : 'Update Group' }} <small>{{ $mode === 'update' ? $group->name : null }}</small></h1>
</div>

<form method="post" action="">

<div class="form-group">
<div class="form-group{{ $errors->first('name', ' has-error') }}">

<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" value="{{ Input::old('name', $group->name) }}" placeholder="Enter the group name">

<input type="text" class="form-control" name="name" id="name" value="{{ Input::old('name', $group->name) }}" placeholder="Enter the group name.">

<span class="help-block">{{{ $errors->first('name', ':message') }}}</span>

</div>

<div class="form-group">
<div class="form-group{{ $errors->first('slug', ' has-error') }}">

<label for="slug">Slug</label>
<input type="text" class="form-control" name="slug" id="slug" value="{{ Input::old('slug', $group->slug) }}" placeholder="Enter the group name">

<input type="text" class="form-control" name="slug" id="slug" value="{{ Input::old('slug', $group->slug) }}" placeholder="Enter the group slug.">

<span class="help-block">{{{ $errors->first('slug', ':message') }}}</span>

</div>

<button type="submit" class="btn btn-default">Submit</button>
Loading

0 comments on commit b40e623

Please sign in to comment.