|
查看: 2234|回复: 11
|
關于php mvc framework的acl 權限設定
[复制链接]
|
|
|
本帖最后由 雷洛 于 11-2-2010 08:48 AM 编辑
研究了cakephp 官網的acl 一段時間,也實做成功
但發現官網的教學, 尚缺一項功能.
官網教學蠻長的 ,所以付上連接,
http://book.cakephp.org/view/641/Simple-Acl-controlled-Application
就是扮演user的這個role,不能設定為僅修改自己的文章,
後來發現一篇教學,內容是寫一個function 來檢查該user是不是文章的發佈者.
http://aranworld.com/article/189/cake ... level-protection-and-crud
- function checkUsersOwnRecord($recordId = null)
- { if( $this->Auth->user('id') == $recordId )
- { return TRUE; }
- else { return FALSE; } }
复制代码
我根據了這個藍本.稍微的修改了一下.
雖然還有些bug,但也確實實現了 , 只有發布者才能修改自己的文章這個功能.
- function edit($id = null) {
- if (!$id && empty($this->data)) {
- $this->Session->setFlash(__('Invalid Post', true));
- $this->redirect(array('action'=>'index'));
- }
- if (!empty($this->data)) {
- $edit_permit=$this->data['Post']['user_id']; //use user_id to distinguish the user only can edit his own article
- //and other user role don't hav the permission to edit ,
- echo "______".$edit_permit."_______";
- //just want to make sure , we are in the way of edit this post
- if($this->checkUser($edit_permit)==TRUE){
- if ($this->Post->save($this->data)) {
- $this->Session->setFlash(__('The Post has been saved',$edit_permit, true));
- //orginal $this->redirect(array('action'=>'index'));
- } else {
- $this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
- }
- }
- else {
- $this->redirect(array('action'=>'index'));
- $this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
- }
- }
- if (empty($this->data)) {
- $this->data = $this->Post->read(null, $id);
- }
- $users = $this->Post->User->find('list');
- $this->set(compact('users'));
- }
复制代码
不過相同的程式碼帶入view 的這個function, 卻發現了問題 ,希望有人能為我解惑, 感謝
- function view($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid Post.', true));
- $this->redirect(array('action'=>'index'));
- }
- else{
- $edit_permit=13; //use user_id to disguish the user only can edit his own article
- //這裡的$edit_permit用13 是因為我目前的user_id是13, 我嘗試用$this->data['Post']['user_id'];卻顯示空白,
- echo "--->".$edit_permit."<---";
- if($this->checkUser($edit_permit)==TRUE){
- $this->set('post', $this->Post->read(null, $id));
- }
- else
- {$this->redirect(array('action'=>'index'));}
- }
- }
- 另外checkUser Function如下,
- function checkUser($recordId=null){
- if($this->Auth->user('id') == $recordId){return TRUE;}
- else {return FALSE;}
- }
复制代码
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 9-2-2010 04:54 PM
|
显示全部楼层
本帖最后由 雷洛 于 9-2-2010 04:56 PM 编辑
還是有人有關于acl permission更好的解決方法嗎@@? 或是更簡單的framework(ACL 實現)推薦嗎? 又或者其他的php framework 對於acl 的權限是如何處理的(個別的member user 不能互相edit 彼此的文章)
比如john(user 1) , 不能修改 Mary(user 2) 的帖子.
網路上看到的文章多半區分了 admin 跟 member user 的權限
但是個別的member user 權限卻沒精確的限定member之間 不能互相edit 彼此的文章.
有人說寫個function 就能區分是不是自己的文章 , 不過我多次測試卻還是存在問題,
可以的話 , 最好有source code...
= = 有點考慮換去學code igniter了 , 不想跟cakephp玩了 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 9-2-2010 05:12 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 10-2-2010 01:02 AM
|
显示全部楼层
|
个人觉得。。。cakephp的api好像都不是很齐全 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-2-2010 02:37 PM
|
显示全部楼层
我只覺得她的教學文件很濫 , 學了幾個月才有小成,
一度跑去找其他framework , QEEPHP(中國的一個PHP framework) ,後來感覺到她好像沒甚麼前景. 似乎沒繼續開發,
現在又想去看看code igniter, 不過不同框架都看看 , 以前看不懂的東西也突然明白了,
現在想寫大一點的php cms , 希望code igniter 能夠幫到忙 ,
Code Igniter的教學文件真棒~ |
|
|
|
|
|
|
|
|
|
|
发表于 10-2-2010 03:38 PM
|
显示全部楼层
|
每个 framework 都有自己一定的模式,只不过看你是否能够适应而已。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-2-2010 10:37 PM
|
显示全部楼层
我對他的能力是沒甚麼懷疑 , 除了她的文件沒有連貫性, 像api文件多過tutorial , 當然也可能我的第一個mvc framework 是學cakephp ,所以進度緩慢.
已經不年輕了, 要選容易上手的東西了~
學習能力下降中 |
|
|
|
|
|
|
|
|
|
|
发表于 11-2-2010 12:15 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 11-2-2010 02:46 PM
|
显示全部楼层
symfony ?
本人刚学着。。。很难入手咯~~~
(PHP 1 个月的经验)  |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-2-2010 06:00 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-2-2010 10:27 AM
|
显示全部楼层
本帖最后由 雷洛 于 27-2-2010 10:38 AM 编辑
終於給我找到了解決的方法了
ibm的某篇教學解決了我的問題(實際上這篇非常完整 , 雖然我還是有還沒想通的地方)
LINK:http://www.ibm.com/developerworks/opensource/tutorials/os-php-cake2/section8.html
User的權限assign (ARO) .
- function register() {
- if (!empty($this->data))
- {
- $this->data['User']['password'] = md5($this->data['User']
- ['password']);
- if ($this->User->save($this->data))
- {
- $this->Session->setFlash('Your registration information
- was accepted');
- $this->Session->write('user', $this->data['User']['username']);
- $parent = $this->Acl->Aro->findByAlias('Users');
- $aro = new Aro();
- $aro->create();
- $aro->save(array(
- 'alias' => $this->data['User']['username'],
- 'model' => 'User',
- 'foreign_key' => $this->User->id,
- 'parent_id' => $parent['Aro']['id'])
- );
- $this->Acl->Aro->save();
- $this->redirect(array('action' => 'index'), null, true);
- } else {
- $this->data['User']['password'] = '';
- $this->Session->setFlash('There was a problem saving
- this information');
- }
- }
- }
-
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-2-2010 10:33 AM
|
显示全部楼层
本帖最后由 雷洛 于 27-2-2010 10:37 AM 编辑
PRODUCT的權限assign (ACO)-
-
- function add() {
- if (!empty($this->data)) {
- $this->Product->create();
- if ($this->Product->save($this->data)) {
- $dealer = $this->Product->Dealer->read(null,
- $this->data['Product']['dealer_id']);
- $parent = $this->Acl->Aco->findByAlias(
- $dealer['Dealer']['title']);
- $alias = $this->Product->id.'-'.$this->data
- ['Product']['title'];
- $aco = new Aco();
- $aco->create();
- $aco->save(array(
- 'alias' => $alias,
- 'model' => 'Product',
- 'foreign_key' => $this->Product->id,
- 'parent_id' => $parent['Aco']['id']
- ));
- $this->Acl->allow('Users', $alias, 'read');
- $this->Acl->allow($this->Session->read('user'), $alias);
- $this->Session->setFlash(__('The Product has been saved', true));
- $this->redirect(array('action'=>'index'));
- } else {
- $this->Session->setFlash(__('The Product could not be saved.
- Please, try again.', true));
- }
- }
- $dealers = $this->Product->Dealer->find('list');
- $this->set(compact('dealers'));
- }
-
-
复制代码 最後的權限實現 檢查VIEW
- function view($id = null) {if (!$id) {
- $this->Session->setFlash('Invalid Product.', true));
- $this->redirect(array('action'=>'index'));
- }
- $product = $this->Product->read(null, $id);
- $alias = $id . '-' . $product['Product']['title'];
- if ($this->Acl->check($this->Session->read('user'), $alias;
- $id . '-' . $product['Product']['title'], 'read')) {
- $this->set('product', $product);
- } else {
- $this->Session->setFlash('Only registered users may view this product.');
- $this->redirect(array('action'=>'index'));
- }
- }
-
复制代码 EDIT- function edit($id = null) {
- if (!$id && empty($this->data)) {
- $this->Session->setFlash(__('Invalid Product', true));
- $this->redirect(array('action'=>'index'));
- }
- $product = $this->Product->read(null, $id);
- $alias = $id.'-'.$product['Product']['title'];
- if ($this->Acl->check($this->Session->read('user'),
- $alias, 'update')) {
- if (!empty($this->data)) {
- if ($this->Product->save($this->data)) {
- $this->Session->setFlash(__('The Product has been saved',
- true));
- $this->redirect(array('action'=>'index'));
- } else {
- $this->Session->setFlash(__('The Product could not be saved.
- Please, try again.', true));
- }
- }
- if (empty($this->data)) {
- $this->data = $this->Product->read(null, $id);
- }
- } else {
- $this->Session->setFlash('You cannot edit this product.');
- $this->redirect(array('action'=>'index'), null, true);
- }
- $dealers = $this->Product->Dealer->find('list');
- $this->set(compact('dealers'));
- }
复制代码 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|