在 ORM 中连接表
我正在尝试在 Kohana 中编写自己的 RBAC 模块。 我不想使用现有的模块,我只是想这样做来学习。
我的表是:用户、角色、权限和users_roles、roles_permissions(因为用户<->角色和角色<->权限之间存在多对多关系);
我的用户模型:
class Model_User extends ORM {
protected $_primary_key = 'user_id';
protected $_has_many = array(
'roles' => array(
'model' => 'role',
'through' => 'users_roles',
),
);
}
我的角色模型:
class Model_Role extends ORM {
protected $_primary_key = 'role_id';
protected $_has_many = array(
'users' => array(
'model' => 'user',
'through' => 'users_roles',
),
'permissions' => array(
'model' => 'permission',
'through' => 'roles_permissions',
),
);
}
和我的权限模型:
class Model_Permission extends ORM {
protected $_primary_key = 'permission_id';
protected $_has_many = array(
'roles' => array(
'model' => 'role',
'through' => 'roles_permissions',
),
);
}
我创建用户:
$user = ORM::factory('user');
$user->username = 'johndoe';
$user->email = '[email protected]';
//etc
$user.save();
我创建角色:
$author = ORM::factory('role');
$author->name = 'author';
$author->save();
我创建权限:
$read = ORM::factory('permission');
$read->name = 'read';
$read->description = 'can read posts';
$read->save();
$write = ORM::factory('permission');
$write->name = 'write';
$write->description = 'can write posts';
$write->save();
我向用户添加角色:
$user->add('roles', $author);
我向角色添加权限:
$author->add('permissions', $read);
$author->add('permissions', $write);
一切正常。
但是,我的问题是如何检查用户是否具有给定的权限:在本例中如何检查johndoe是否有权写入邮政?
感谢您的帮助!
I'm trying to write my own RBAC module in Kohana.
I don't want to use an existing module, I just want to do this to learn.
My tables are: users, roles, permission and users_roles, roles_permissions (because of the many to many relations between users<->roles, and roles<->permissions);
My User model:
class Model_User extends ORM {
protected $_primary_key = 'user_id';
protected $_has_many = array(
'roles' => array(
'model' => 'role',
'through' => 'users_roles',
),
);
}
My Role Model:
class Model_Role extends ORM {
protected $_primary_key = 'role_id';
protected $_has_many = array(
'users' => array(
'model' => 'user',
'through' => 'users_roles',
),
'permissions' => array(
'model' => 'permission',
'through' => 'roles_permissions',
),
);
}
and my Permission model:
class Model_Permission extends ORM {
protected $_primary_key = 'permission_id';
protected $_has_many = array(
'roles' => array(
'model' => 'role',
'through' => 'roles_permissions',
),
);
}
I create users:
$user = ORM::factory('user');
$user->username = 'johndoe';
$user->email = '[email protected]';
//etc
$user.save();
I create roles:
$author = ORM::factory('role');
$author->name = 'author';
$author->save();
I create permissions:
$read = ORM::factory('permission');
$read->name = 'read';
$read->description = 'can read posts';
$read->save();
$write = ORM::factory('permission');
$write->name = 'write';
$write->description = 'can write posts';
$write->save();
I add roles to users:
$user->add('roles', $author);
I add permissions to roles:
$author->add('permissions', $read);
$author->add('permissions', $write);
and everything is working fine.
But, my question is how to check if a user has a given permission: in this case how to check if johndoe has permission to write a post?
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个查询,但与上一个答案相比只有两个连接
}
One query, but only two joins as compared to the previous answer
}
我在 1 个查询中找到了一个解决方案:
然后 if
$count > 0
,表示$user有写入权限。有更简单的解决方案吗?
I found a solution, in 1 query:
and then if
$count > 0
, it means $user has permission to write.Is there a simpler solution?
这是一个更好的解决方案,当提供主键或模型实例时,它将执行更快的查询:
Here is a better solution which will perform a faster query when primary key or a model instance is supplied: