Commit cf4bb7d7 authored by Nicolas Widart's avatar Nicolas Widart

Squashed 'Modules/User/' changes from a0751eb..d4e1225

d4e1225 Merge pull request #13 from AsgardCms/change-sentry-implementation
b0614b7 Setting the foreign key on the group relation
a690f1c Return current user if logged in
e89bcd1 Returning the current user if logged in
dae64bf Catching sentry exceptions
1eb05f3 Setting a foreignkey
b03872c Pass the activated boolean
f616981 Able to add multiple roles to user.
6a1fd50 Add a sentry group seeder
8292b2c Set sentry as default driver
ce685ac Change seed names to sentinel
eca5547 Add more basic permissions
4b00958 Merge commit '0477f484'
c664344 Merge commit 'a10a0c1b'
191cd91 Merge commit 'e5bf1d57'
b69959b Merge commit '4273e495'
fd78d2c Merge commit '24d99222'
28c3793 Merge commit '93831760'
ebf382d Merge commit '99ca8493'
fa9a4a4 Merge commit 'b7a4a21b'
b82249d Merge commit 'e7005afe'
c1ad008 Merge commit '97214232'
a8e2297 Merge commit '1ff70b6d'
3ab1534 Merge commit '6994f095'

git-subtree-dir: Modules/User
git-subtree-split: d4e12253a934089519e2e668f32f905507e52a6f
parent 0477f484
<?php namespace Modules\User\Database\Seeders;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class SentinelGroupSeedTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$groups = Sentinel::getRoleRepository();
// Create an Admin group
$groups->createModel()->create(
[
'name' => 'Admin',
'slug' => 'admin',
]
);
// Create an Users group
$groups->createModel()->create(
[
'name' => 'User',
'slug' => 'user',
]
);
// Save the permissions
$group = Sentinel::findRoleBySlug('admin');
$group->permissions = [
'dashboard.index' => true,
/* Workbench */
'workbench.index' => true,
'workbench.generate' => true,
'workbench.migrate' => true,
'workbench.install' => true,
'workbench.seed' => true,
'modules.index' => true,
'modules.store' => true,
'generate.generate' => true,
'install.install' => true,
'migrate.migrate' => true,
'seed.seed' => true,
/* Roles */
'roles.index' => true,
'roles.create' => true,
'roles.store' => true,
'roles.edit' => true,
'roles.update' => true,
'roles.destroy' => true,
/* Users */
'users.index' => true,
'users.create' => true,
'users.store' => true,
'users.edit' => true,
'users.update' => true,
'users.destroy' => true,
/* Menu */
'menus.index' => true,
'menus.create' => true,
'menus.store' => true,
'menus.edit' => true,
'menus.update' => true,
'menus.destroy' => true,
'menuitem.index' => true,
'menuitem.create' => true,
'menuitem.store' => true,
'menuitem.edit' => true,
'menuitem.update' => true,
'menuitem.destroy' => true,
/* Media */
'media.index' => true,
'media.create' => true,
'media.store' => true,
'media.edit' => true,
'media.update' => true,
'media.destroy' => true,
];
$group->save();
$group = Sentinel::findRoleBySlug('user');
$group->permissions = [
'dashboard.index' => true
];
$group->save();
}
}
...@@ -5,7 +5,7 @@ use Cartalyst\Sentinel\Laravel\Facades\Sentinel; ...@@ -5,7 +5,7 @@ use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class SentryUserSeedTableSeeder extends Seeder class SentinelUserSeedTableSeeder extends Seeder
{ {
/** /**
* Run the database seeds. * Run the database seeds.
......
<?php namespace Modules\User\Database\Seeders; <?php namespace Modules\User\Database\Seeders;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel; use Cartalyst\Sentry\Facades\Laravel\Sentry;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
...@@ -15,55 +15,68 @@ class SentryGroupSeedTableSeeder extends Seeder ...@@ -15,55 +15,68 @@ class SentryGroupSeedTableSeeder extends Seeder
{ {
Model::unguard(); Model::unguard();
$groups = Sentinel::getRoleRepository();
// Create an Admin group // Create an Admin group
$groups->createModel()->create( Sentry::createGroup(array(
[ 'name' => 'Admin',
'name' => 'Admin', 'permissions' => [
'slug' => 'admin', 'dashboard.index' => 1,
] /* Workbench */
); 'workbench.index' => 1,
'workbench.generate' => 1,
'workbench.migrate' => 1,
'workbench.install' => 1,
'workbench.seed' => 1,
'modules.index' => 1,
'modules.store' => 1,
'generate.generate' => 1,
'install.install' => 1,
'migrate.migrate' => 1,
'seed.seed' => 1,
/* Roles */
'roles.index' => 1,
'roles.create' => 1,
'roles.store' => 1,
'roles.edit' => 1,
'roles.update' => 1,
'roles.destroy' => 1,
/* Users */
'users.index' => 1,
'users.create' => 1,
'users.store' => 1,
'users.edit' => 1,
'users.update' => 1,
'users.destroy' => 1,
/* Menu */
'menus.index' => 1,
'menus.create' => 1,
'menus.store' => 1,
'menus.edit' => 1,
'menus.update' => 1,
'menus.destroy' => 1,
'menuitem.index' => 1,
'menuitem.create' => 1,
'menuitem.store' => 1,
'menuitem.edit' => 1,
'menuitem.update' => 1,
'menuitem.destroy' => 1,
/* Media */
'media.index' => 1,
'media.create' => 1,
'media.store' => 1,
'media.edit' => 1,
'media.update' => 1,
'media.destroy' => 1,
],
));
// Create an Users group // Create an Users group
$groups->createModel()->create( Sentry::createGroup(array(
[ 'name' => 'User',
'name' => 'User', 'permissions' => [
'slug' => 'user', 'dashboard.index' => 1,
] ],
); ));
// Save the permissions
$group = Sentinel::findRoleBySlug('admin');
$group->permissions = [
'dashboard.index' => true,
'workbench.index' => true,
'workbench.generate' => true,
'workbench.migrate' => true,
'workbench.install' => true,
'workbench.seed' => true,
'modules.index' => true,
'modules.store' => true,
'roles.index' => true,
'roles.create' => true,
'roles.store' => true,
'roles.edit' => true,
'roles.update' => true,
'roles.destroy' => true,
'users.index' => true,
'users.create' => true,
'users.store' => true,
'users.edit' => true,
'users.update' => true,
'users.destroy' => true,
];
$group->save();
$group = Sentinel::findRoleBySlug('user');
$group->permissions = [
'dashboard.index' => true
];
$group->save();
} }
} }
...@@ -14,7 +14,7 @@ class UserDatabaseSeeder extends Seeder ...@@ -14,7 +14,7 @@ class UserDatabaseSeeder extends Seeder
{ {
Model::unguard(); Model::unguard();
$this->call("Modules\\User\\Database\\Seeders\\SentryGroupSeedTableSeeder"); $this->call("Modules\\User\\Database\\Seeders\\SentinelGroupSeedTableSeeder");
} }
} }
...@@ -16,4 +16,9 @@ class SentryUser extends User ...@@ -16,4 +16,9 @@ class SentryUser extends User
]; ];
protected $presenter = 'Modules\User\Presenters\UserPresenter'; protected $presenter = 'Modules\User\Presenters\UserPresenter';
public function groups()
{
return $this->belongsToMany(static::$groupModel, static::$userGroupsPivot, 'user_id');
}
} }
...@@ -45,3 +45,4 @@ class RouteServiceProvider extends ServiceProvider ...@@ -45,3 +45,4 @@ class RouteServiceProvider extends ServiceProvider
} }
} }
...@@ -76,15 +76,15 @@ class UserServiceProvider extends ServiceProvider ...@@ -76,15 +76,15 @@ class UserServiceProvider extends ServiceProvider
{ {
$this->app->bind( $this->app->bind(
'Modules\User\Repositories\UserRepository', 'Modules\User\Repositories\UserRepository',
'Modules\User\Repositories\Sentinel\SentinelUserRepository' 'Modules\User\Repositories\Sentry\SentryUserRepository'
); );
$this->app->bind( $this->app->bind(
'Modules\User\Repositories\RoleRepository', 'Modules\User\Repositories\RoleRepository',
'Modules\User\Repositories\Sentinel\SentinelRoleRepository' 'Modules\User\Repositories\Sentry\SentryRoleRepository'
); );
$this->app->bind( $this->app->bind(
'Modules\Core\Contracts\Authentication', 'Modules\Core\Contracts\Authentication',
'Modules\User\Repositories\Sentinel\SentinelAuthentication' 'Modules\User\Repositories\Sentry\SentryAuthentication'
); );
} }
......
<?php namespace Modules\User\Repositories\Sentry; <?php namespace Modules\User\Repositories\Sentry;
use Cartalyst\Sentry\Throttling\UserBannedException;
use Cartalyst\Sentry\Throttling\UserSuspendedException;
use Cartalyst\Sentry\Users\LoginRequiredException;
use Cartalyst\Sentry\Users\PasswordRequiredException;
use Cartalyst\Sentry\Users\UserNotActivatedException;
use Cartalyst\Sentry\Users\UserNotFoundException;
use Cartalyst\Sentry\Users\WrongPasswordException;
use Modules\Core\Contracts\Authentication; use Modules\Core\Contracts\Authentication;
use Cartalyst\Sentry\Facades\Laravel\Sentry; use Cartalyst\Sentry\Facades\Laravel\Sentry;
...@@ -13,9 +20,40 @@ class SentryAuthentication implements Authentication ...@@ -13,9 +20,40 @@ class SentryAuthentication implements Authentication
*/ */
public function login(array $credentials, $remember = false) public function login(array $credentials, $remember = false)
{ {
if (Sentry::authenticate($credentials, $remember)) { try
{
Sentry::authenticate($credentials, $remember);
return false; return false;
} }
catch (LoginRequiredException $e)
{
return 'Login field is required.';
}
catch (PasswordRequiredException $e)
{
return 'Password field is required.';
}
catch (WrongPasswordException $e)
{
return 'Wrong password, try again.';
}
catch (UserNotFoundException $e)
{
return 'User was not found.';
}
catch (UserNotActivatedException $e)
{
return 'User is not activated.';
}
catch (UserSuspendedException $e)
{
return 'User is suspended.';
}
catch (UserBannedException $e)
{
return 'User is banned.';
}
return 'Invalid login or password.'; return 'Invalid login or password.';
} }
...@@ -110,6 +148,9 @@ class SentryAuthentication implements Authentication ...@@ -110,6 +148,9 @@ class SentryAuthentication implements Authentication
*/ */
public function check() public function check()
{ {
return Sentry::check(); if (Sentry::check()) {
return Sentry::getUser();
}
return false;
} }
} }
...@@ -28,16 +28,17 @@ class SentryUserRepository implements UserRepository ...@@ -28,16 +28,17 @@ class SentryUserRepository implements UserRepository
* Create a user and assign roles to it * Create a user and assign roles to it
* @param array $data * @param array $data
* @param array $roles * @param array $roles
* @return void * @param bool $activated
*/ */
public function createWithRoles($data, $roles) public function createWithRoles($data, $roles, $activated = false)
{ {
$user = Sentry::createUser($data); $user = Sentry::createUser(array_merge($data, ['activated' => $activated]));
if (!empty($roles)) { if (!empty($roles)) {
$group = Sentry::findGroupByName($roles); foreach ($roles as $roleId) {
$user->addGroup($group); $group = Sentry::findGroupById($roleId);
$user->addGroup($group);
}
} }
$user->attemptActivation($user->getActivationCode());
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment