Skip to content

Commit af97a7a

Browse files
committed
Update Auth Filters
1 parent fae01ba commit af97a7a

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

app/controllers/GroupController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(GroupInterface $group, GroupForm $groupForm)
2020
$this->groupForm = $groupForm;
2121

2222
// Establish Filters
23-
$this->beforeFilter('admin_auth');
23+
$this->beforeFilter('inGroup:Admins');
2424
}
2525

2626
/**

app/controllers/UserController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public function __construct(
4646
$this->beforeFilter('csrf', array('on' => 'post'));
4747

4848
// Set up Auth Filters
49-
$this->beforeFilter('auth', array('except' => array('create', 'store', 'activate', 'resend', 'forgot', 'reset')));
49+
$this->beforeFilter('auth', array('only' => array('show', 'update', 'change')));
50+
$this->beforeFilter('inGroup:Admins', array('only' => array('index', 'destroy', 'suspend', 'unsuspend', 'ban', 'unban', 'edit')));
51+
//array('except' => array('create', 'store', 'activate', 'resend', 'forgot', 'reset')));
5052
}
5153

5254

app/filters.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,42 @@
3535

3636
Route::filter('auth', function()
3737
{
38-
if (!Sentry::check()) return Redirect::to('login');
38+
if (!Sentry::check()) return Redirect::route('login');
3939
});
4040

41-
Route::filter('admin_auth', function()
41+
Route::filter('inGroup', function($route, $request, $value)
4242
{
43-
if (!Sentry::check())
43+
if (!Sentry::check()) return Redirect::route('login');
44+
45+
// we need to determine if a non admin user
46+
// is trying to access their own account.
47+
$userId = $route->getParameter('users');
48+
49+
try
4450
{
45-
// if not logged in, redirect to login
46-
return Redirect::to('login');
51+
$user = Sentry::getUser();
52+
53+
$group = Sentry::findGroupByName($value);
54+
55+
if ($userId != Session::get('userId') && (! $user->inGroup($group)) )
56+
{
57+
Session::flash('error', trans('users.noaccess'));
58+
return Redirect::route('home');
59+
}
4760
}
48-
49-
if (!Sentry::getUser()->hasAccess('admin'))
61+
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
62+
{
63+
Session::flash('error', trans('users.notfound'));
64+
return Redirect::route('login');
65+
}
66+
67+
catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
5068
{
51-
// has no access
52-
return Response::make('Access Forbidden', '403');
69+
Session::flash('error', trans('groups.notfound'));
70+
return Redirect::route('login');
5371
}
5472
});
73+
// thanks to http://laravelsnippets.com/snippets/sentry-route-filters
5574

5675
/*
5776
|--------------------------------------------------------------------------

app/lang/en/users.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
'notfound' => "User not found",
1818

19+
'noaccess' => "You are not allowed to do that.",
20+
1921
'updated' => "Profile updated",
2022

2123
'notupdated' => "Unable to update profile",

app/routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
// Group Routes
4646
Route::resource('groups', 'GroupController');
4747

48-
Route::get('/', function()
48+
Route::get('/', array('as' => 'home', function()
4949
{
5050
return View::make('home');
51-
});
51+
}));
5252

5353

5454
// App::missing(function($exception)

0 commit comments

Comments
 (0)