"zizaco/confide": "~4.0@dev"と"zizaco/entrust": "1.2.*@dev"を使用しています。
2 つのチュートリアル (confide migrations) で説明されているように、すべてをセットアップしました。さらに、次のモデルを作成しました。
ユーザー:
<?php
use Zizaco\Confide\ConfideUser;
use Zizaco\Confide\Confide;
use Zizaco\Confide\ConfideEloquentRepository;
use Zizaco\Entrust\HasRole;
use Carbon\Carbon;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface{
use ConfideUser;
use HasRole;
/**
* Get user by username
* @param $username
* @return mixed
*/
public function getUserByUsername( $username )
{
return $this->where('username', '=', $username)->first();
}
public function joined()
{
return String::date(Carbon::createFromFormat('Y-n-j G:i:s', $this->created_at));
}
public function saveRoles($inputRoles)
{
if(! empty($inputRoles)) {
$this->roles()->sync($inputRoles);
} else {
$this->roles()->detach();
}
}
public function currentRoleIds()
{
$roles = $this->roles;
$roleIds = false;
if( !empty( $roles ) ) {
$roleIds = array();
foreach( $roles as &$role )
{
$roleIds[] = $role->id;
}
}
return $roleIds;
}
public static function checkAuthAndRedirect($redirect, $ifValid=false)
{
// Get the user information
$user = Auth::user();
$redirectTo = false;
if(empty($user->id) && ! $ifValid) // Not logged in redirect, set session.
{
Session::put('loginRedirect', $redirect);
$redirectTo = Redirect::to('user/login')
->with( 'notice', Lang::get('user/user.login_first') );
}
elseif(!empty($user->id) && $ifValid) // Valid user, we want to redirect.
{
$redirectTo = Redirect::to($redirect);
}
return array($user, $redirectTo);
}
public function currentUser()
{
return (new Confide(new ConfideEloquentRepository()))->user();
}
public function getReminderEmail()
{
return $this->email;
}
}
役割:
<?php
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole {
public function validateRoles( array $roles )
{
$user = Confide::user();
$roleValidation = new stdClass();
foreach( $roles as $role )
{
// Make sure theres a valid user, then check role.
$roleValidation->$role = ( empty($user) ? false : $user->hasRole($role) );
}
return $roleValidation;
}
}
許可:
<?php
use Zizaco\Entrust\EntrustPermission;
class Permission extends EntrustPermission
{
public function preparePermissionsForDisplay($permissions)
{
// Get all the available permissions
$availablePermissions = $this->all()->toArray();
foreach($permissions as &$permission) {
array_walk($availablePermissions, function(&$value) use(&$permission){
if($permission->name == $value['name']) {
$value['checked'] = true;
}
});
}
return $availablePermissions;
}
/**
* Convert from input array to savable array.
* @param $permissions
* @return array
*/
public function preparePermissionsForSave( $permissions )
{
$availablePermissions = $this->all()->toArray();
$preparedPermissions = array();
foreach( $permissions as $permission => $value )
{
// If checkbox is selected
if( $value == '1' )
{
// If permission exists
array_walk($availablePermissions, function(&$value) use($permission, &$preparedPermissions){
if($permission == (int)$value['id']) {
$preparedPermissions[] = $permission;
}
});
}
}
return $preparedPermissions;
}
}
さらに、最初にデータベースに値をシードしたいので、ユーザー、ロール、および権限用にいくつかのシーダーを作成しました。ただし、許可シーダーでエラーが発生します。
UserTableSeeder:
<?php
class UsersTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
$users = array(
array(
'username' => 'admin',
'email' => 'admin@example.org',
'password' => Hash::make('admin'),
'confirmed' => 1,
'confirmation_code' => md5(microtime().Config::get('app.key')),
'created_at' => new DateTime,
'updated_at' => new DateTime,
),
array(
'username' => 'moderator',
'email' => 'moderator@example.org',
'password' => Hash::make('moderator'),
'confirmed' => 1,
'confirmation_code' => md5(microtime().Config::get('app.key')),
'created_at' => new DateTime,
'updated_at' => new DateTime,
),
array(
'username' => 'user',
'email' => 'user@example.org',
'password' => Hash::make('user'),
'confirmed' => 1,
'confirmation_code' => md5(microtime().Config::get('app.key')),
'created_at' => new DateTime,
'updated_at' => new DateTime,
)
);
DB::table('users')->insert( $users );
}
}
RolesTableSeeder:
<?php
class RolesTableSeeder extends Seeder {
public function run()
{
DB::table('roles')->delete();
$adminRole = new Role;
$adminRole->name = 'adminRole';
$adminRole->save();
$standRole = new Role;
$standRole->name = 'userRole';
$standRole->save();
$modRole = new Role;
$modRole->name = 'modRole';
$modRole->save();
$user = User::where('username','=','admin')->first();
$user->attachRole( $adminRole );
$user = User::where('username','=','user')->first();
$user->attachRole( $standRole );
$user = User::where('username','=','moderator')->first();
$user->attachRole( $modRole );
}
}
PermissionsTableSeeder:
<?php
class PermissionsTableSeeder extends Seeder {
public function run()
{
DB::table('permissions')->delete();
$permissions = array(
array( // 1
'name' => 'manage_users',
'display_name' => 'manage users'
),
array( // 2
'name' => 'manage_roles',
'display_name' => 'manage roles'
),
array( // 3
'name' => 'standart_user_role',
'display_name' => 'standart_user_role'
),
);
DB::table('permissions')->insert( $permissions );
DB::table('permission_role')->delete();
$role_id_admin = Role::where('name', '=', 'admin')->first()->id;
$role_id_mod = Role::where('name', '=', 'moderator')->first()->id;
$role_id_stand = Role::where('name', '=', 'user')->first()->id;
$permission_base = (int)DB::table('permissions')->first()->id - 1;
$permissions = array(
array(
'role_id' => $role_id_admin,
'permission_id' => $permission_base + 1
),
array(
'role_id' => $role_id_admin,
'permission_id' => $permission_base + 2
),
array(
'role_id' => $role_id_mod,
'permission_id' => $permission_base + 1
),
array(
'role_id' => $role_id_mod,
'permission_id' => $permission_base + 3
),
array(
'role_id' => $role_id_stand,
'permission_id' => $permission_base + 3
),
);
DB::table('permission_role')->insert( $permissions );
}
}
これは、実行時に表示されるエラーですdb:seed
。
$ php artisan db:seed
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? Y
Seeded: UsersTableSeeder
Seeded: RolesTableSeeder
[ErrorException]
Trying to get property of non-object
db:seed [--class[="..."]] [--database[="..."]] [--force]
シーディングで間違っていることについての推奨事項はありますか?
あなたの答えに感謝します!