mirror of
https://gitlab.com/omair-personal/exchange.git
synced 2026-08-19 04:24:08 +00:00
added seeder
added user to return role
This commit is contained in:
+12
-1
@@ -38,7 +38,7 @@ class User extends Authenticatable implements JWTSubject
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = [
|
||||
'photo_url',
|
||||
'photo_url', 'role'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -51,6 +51,16 @@ class User extends Authenticatable implements JWTSubject
|
||||
return 'https://www.gravatar.com/avatar/'.md5(strtolower($this->email)).'.jpg?s=200&d=mm';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the role attribute.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRoleAttribute()
|
||||
{
|
||||
return $this->roles->first()->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the oauth providers.
|
||||
*
|
||||
@@ -87,4 +97,5 @@ class User extends Authenticatable implements JWTSubject
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,22 +11,9 @@ class DatabaseSeeder extends Seeder
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// $this->call(UsersTableSeeder::class);
|
||||
Model::unguard();
|
||||
DB::table('users')->delete();
|
||||
$this->call(UsersTableSeeder::class);
|
||||
$this->call(RolesAndPermissionsSeeder::class);
|
||||
|
||||
$users = array(
|
||||
['name' => 'Ryan Chenkie', 'email' => 'ryanchenkie@gmail.com', 'password' => Hash::make('secret')],
|
||||
['name' => 'Chris Sevilleja', 'email' => 'chris@scotch.io', 'password' => Hash::make('secret')],
|
||||
['name' => 'Holly Lloyd', 'email' => 'holly@scotch.io', 'password' => Hash::make('secret')],
|
||||
['name' => 'Adnan Kukic', 'email' => 'adnan@scotch.io', 'password' => Hash::make('secret')],
|
||||
);
|
||||
|
||||
foreach ($users as $user)
|
||||
{
|
||||
User::create($user);
|
||||
}
|
||||
|
||||
Model::reguard();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Roles & Permissions Seeder
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This Seeder class allows you to update and create Roles & Permissions
|
||||
| for the Laravel Entrust package.
|
||||
|
|
||||
| USE -> php artisan db:seed --class=RolesAndPermissionsSeeder
|
||||
|
|
||||
| https://github.com/thomasfw/RolesAndPermissionsSeeder
|
||||
|
|
||||
|--------------------------------------------------------------------------
|
||||
| Make sure you update the namespaces for your User & Entrust models
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
use App\User as User;
|
||||
use App\Role as Role;
|
||||
use App\Permission as Permission;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
class RolesAndPermissionsSeeder extends Seeder {
|
||||
protected $roles = [
|
||||
|
||||
'admin' => [
|
||||
],
|
||||
|
||||
'member' => [
|
||||
],
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
|
||||
protected $permissions = [
|
||||
|
||||
// Add your Permissions here
|
||||
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Roles
|
||||
*
|
||||
* @return array()
|
||||
*/
|
||||
public function roles()
|
||||
{
|
||||
return $this->roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permissions
|
||||
*
|
||||
* @param $name
|
||||
* @return array()
|
||||
*/
|
||||
public function permissions($name = '')
|
||||
{
|
||||
$single = (array_key_exists($name,$this->permissions) ? array($name =>$this->permissions[$name]) : false );
|
||||
return ($name ? $single : $this->permissions);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Run the Seeder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
DB::table(Config::get('entrust.permissions_table'))->delete();
|
||||
|
||||
foreach ($this->roles() as $key => $val) {
|
||||
$this->command->info(" ");
|
||||
$this->command->info('Creating/updating the \''.$key.'\' role');
|
||||
$this->command->info('-----------------------------------------');
|
||||
$val['name'] = $key;
|
||||
$this->reset($val);
|
||||
}
|
||||
$this->cleanup();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reset Role, Permissions & Users
|
||||
*
|
||||
* @param $role
|
||||
* @return void
|
||||
*/
|
||||
public function reset($role)
|
||||
{
|
||||
$commandBullet = ' -> ';
|
||||
|
||||
// The Old Role
|
||||
$originalRole = Role::where('name',$role['name'])->first();
|
||||
if($originalRole) Role::where('id',$originalRole->id)->update(['name' => $role['name'].'__remove']);
|
||||
|
||||
// The New Role
|
||||
$newRole = new Role();
|
||||
$newRole->name = $role['name'];
|
||||
if(isset($role['display_name'])) $newRole->display_name = $role['display_name']; // optional
|
||||
if(isset($role['description'])) $newRole->description = $role['description']; // optional
|
||||
$newRole->save();
|
||||
$this->command->info($commandBullet."Created $role[name] role");
|
||||
|
||||
// Set the Permissions (if they exist)
|
||||
$pcount = 0;
|
||||
if(!empty($role['permissions']))
|
||||
{
|
||||
foreach ($role['permissions'] as $permission_name) {
|
||||
|
||||
$permission = $this->permissions($permission_name);
|
||||
if($permission === false || (!$permission_name)) {
|
||||
$this->command->error($commandBullet."Failed to attach permission '$permission_name'. It does not exist");
|
||||
continue;
|
||||
}
|
||||
|
||||
$newPermission = \Permission::where('name',$permission_name)->first();
|
||||
if (!$newPermission) {
|
||||
$newPermission = new Permission();
|
||||
$newPermission->name = key($permission);
|
||||
if(isset($permission['display_name'])) $newPermission->display_name = $permission['display_name']; // optional
|
||||
if(isset($permission['description'])) $newPermission->description = $permission['description']; // optional
|
||||
$newPermission->save();
|
||||
}
|
||||
$newRole->attachPermission($newPermission);
|
||||
$pcount++;
|
||||
}
|
||||
}
|
||||
$this->command->info($commandBullet."Attached $pcount permissions to $role[name] role");
|
||||
|
||||
// Update old records
|
||||
if ($originalRole)
|
||||
{
|
||||
$userCount = 0;
|
||||
$RoleUsers = DB::table(Config::get('entrust.role_user_table'))->where('role_id',$originalRole->id)->get();
|
||||
foreach ($RoleUsers as $user) {
|
||||
$u = User::where('id',$user->user_id)->first();
|
||||
$u->attachRole($newRole);
|
||||
$userCount++;
|
||||
}
|
||||
$this->command->info($commandBullet."Updated role attachment for $userCount users");
|
||||
|
||||
Role::where('id',$originalRole->id)->delete(); // will also remove old role_user records
|
||||
$this->command->info($commandBullet."Removed the original $role[name] role");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cleanup()
|
||||
* Remove any roles & permissions that have been removed
|
||||
* @return void
|
||||
*/
|
||||
public function cleanup()
|
||||
{
|
||||
$commandBullet = ' -> ';
|
||||
$this->command->info(" ");
|
||||
$this->command->info('Cleaning up roles & permissions:');
|
||||
$this->command->info('--------------------------------');
|
||||
|
||||
$storedRoles = Role::all();
|
||||
if(!empty($storedRoles)) {
|
||||
$definedRoles = $this->roles();
|
||||
foreach ($storedRoles as $role) {
|
||||
if ( !array_key_exists($role->name,$definedRoles) ) {
|
||||
Role::where('name',$role->name)->delete();
|
||||
$this->command->info($commandBullet.'The \''.$role->name.'\' role was removed');
|
||||
}
|
||||
}
|
||||
}
|
||||
$storedPerms = DB::table(Config::get('entrust.permissions_table'))->get();
|
||||
if(!empty($storedPerms)) {
|
||||
$definedPerms = $this->permissions();
|
||||
foreach ($storedPerms as $perm) {
|
||||
if ( !array_key_exists($perm->name,$definedPerms) ) {
|
||||
DB::table(Config::get('entrust.permissions_table'))->where('name',$perm->name)->delete();
|
||||
$this->command->info($commandBullet.'The \''.$perm->name.'\' permission was removed');
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->command->info($commandBullet.'Done');
|
||||
$this->command->info(" ");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\User;
|
||||
|
||||
class UsersTableSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
DB::table('users')->delete();
|
||||
|
||||
$users = array(
|
||||
['name' => 'Ryan Chenkie', 'email' => 'ryanchenkie@gmail.com', 'password' => Hash::make('secret')],
|
||||
['name' => 'Chris Sevilleja', 'email' => 'chris@scotch.io', 'password' => Hash::make('secret')],
|
||||
['name' => 'Holly Lloyd', 'email' => 'holly@scotch.io', 'password' => Hash::make('secret')],
|
||||
['name' => 'Adnan Kukic', 'email' => 'adnan@scotch.io', 'password' => Hash::make('secret')],
|
||||
);
|
||||
|
||||
foreach ($users as $user)
|
||||
{
|
||||
User::create($user);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -29,7 +29,7 @@ export default [
|
||||
{ path: 'password', name: 'settings.password', component: SettingsPassword }
|
||||
] },
|
||||
|
||||
{ path: '/admin', name: 'home', component: Home },
|
||||
{ path: '/admin', name: 'admin.home', component: Admin },
|
||||
|
||||
{ path: '*', component: NotFound }
|
||||
]
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ Route::group(['middleware' => 'auth:api'], function () {
|
||||
Route::post('logout', 'Auth\LoginController@logout');
|
||||
|
||||
Route::get('/user', function (Request $request) {
|
||||
return $request->user()->load('roles');
|
||||
return $request->user();
|
||||
});
|
||||
|
||||
Route::patch('settings/profile', 'Settings\ProfileController@update');
|
||||
|
||||
Reference in New Issue
Block a user