佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 2234|回复: 11

關于php mvc framework的acl 權限設定

[复制链接]
发表于 9-2-2010 04:42 PM | 显示全部楼层 |阅读模式
本帖最后由 雷洛 于 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

  1. function checkUsersOwnRecord($recordId = null)
  2. { if( $this->Auth->user('id') == $recordId )
  3. { return TRUE; }
  4. else { return FALSE; } }
复制代码





我根據了這個藍本.稍微的修改了一下.
雖然還有些bug,但也確實實現了 , 只有發布者才能修改自己的文章這個功能.






  1. function edit($id = null) {





  2. if (!$id && empty($this->data)) {
  3. $this->Session->setFlash(__('Invalid Post', true));
  4. $this->redirect(array('action'=>'index'));
  5. }


  6. if (!empty($this->data)) {

  7. $edit_permit=$this->data['Post']['user_id']; //use user_id to distinguish the user only can edit his own article
  8. //and other user role don't hav the permission to edit ,
  9. echo "______".$edit_permit."_______";
  10. //just want to make sure , we are in the way of edit this post

  11. if($this->checkUser($edit_permit)==TRUE){
  12. if ($this->Post->save($this->data)) {
  13. $this->Session->setFlash(__('The Post has been saved',$edit_permit, true));
  14. //orginal $this->redirect(array('action'=>'index'));

  15. } else {
  16. $this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));

  17. }


  18. }
  19. else {

  20. $this->redirect(array('action'=>'index'));
  21. $this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
  22. }
  23. }


  24. if (empty($this->data)) {
  25. $this->data = $this->Post->read(null, $id);
  26. }
  27. $users = $this->Post->User->find('list');
  28. $this->set(compact('users'));
  29. }

复制代码






不過相同的程式碼帶入view 的這個function, 卻發現了問題 ,希望有人能為我解惑, 感謝




  1. function view($id = null) {





  2. if (!$id) {
  3. $this->Session->setFlash(__('Invalid Post.', true));
  4. $this->redirect(array('action'=>'index'));
  5. }

  6. else{

  7. $edit_permit=13; //use user_id to disguish the user only can edit his own article
  8. //這裡的$edit_permit用13 是因為我目前的user_id是13, 我嘗試用$this->data['Post']['user_id'];卻顯示空白,
  9. echo "--->".$edit_permit."<---";

  10. if($this->checkUser($edit_permit)==TRUE){


  11. $this->set('post', $this->Post->read(null, $id));        
  12. }
  13. else
  14. {$this->redirect(array('action'=>'index'));}
  15. }



  16. }




  17. 另外checkUser Function如下,

  18. function checkUser($recordId=null){

  19. if($this->Auth->user('id') == $recordId){return TRUE;}
  20. else {return FALSE;}

  21. }
复制代码

回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 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 | 显示全部楼层
另外推薦一些acl 的文章(我也還在學習中)
cakephp
http://www.ibm.com/developerwork ... cake2/section8.html

code igniter+zend
http://www.mattstone.me/?p=3

超級完整的的acl 權限管理系統設計
http://net.tutsplus.com/tutorials/php/a-better-login-system/
回复

使用道具 举报

发表于 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 都有自己一定的模式,只不过看你是否能够适应而已。
回复

使用道具 举报

Follow Us
 楼主| 发表于 10-2-2010 10:37 PM | 显示全部楼层
我對他的能力是沒甚麼懷疑 , 除了她的文件沒有連貫性, 像api文件多過tutorial , 當然也可能我的第一個mvc framework 是學cakephp ,所以進度緩慢.

已經不年輕了, 要選容易上手的東西了~
學習能力下降中
回复

使用道具 举报

发表于 11-2-2010 12:15 AM | 显示全部楼层
蛋糕是比较容易明白和容易学啦。。。
但是它也超吃resource的。。如果你的coding没有build好的话

个人看法:
framework对我来说,只是coding control而已。
要学还是学纯php比较好点
当然,用framework来开发一个application确实是比较快
回复

使用道具 举报


ADVERTISEMENT

发表于 11-2-2010 02:46 PM | 显示全部楼层
symfony ?

本人刚学着。。。很难入手咯~~~

(PHP 1 个月的经验)
回复

使用道具 举报

 楼主| 发表于 11-2-2010 06:00 PM | 显示全部楼层
我覺得好的開始是成功的一半, codeigniter 蠻適合初學者的  , 沒太多的設定 ,教學簡單, 慢慢的看到自己的學習進度 ,  感覺非常好....就算php了解不深 , 也不會很難學.

http://net.tutsplus.com/tutorials/php/codeigniter-basics/



另外不能不推薦nettuts 這個網站 , 蠻多很新很棒的effect , 雖然最近部分文章開始收費了,
. 真想加入他們的付費member.

http://tutsplus.com/
http://vector.tutsplus.com/
回复

使用道具 举报

 楼主| 发表于 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) .

  1.      function register()    {
  2. if (!empty($this->data))
  3. {
  4. $this->data['User']['password'] = md5($this->data['User']
  5.                         ['password']);
  6. if ($this->User->save($this->data))
  7. {
  8. $this->Session->setFlash('Your registration information
  9.                                   was accepted');
  10. $this->Session->write('user', $this->data['User']['username']);
  11. $parent = $this->Acl->Aro->findByAlias('Users');
  12.                 $aro = new Aro();
  13.                 $aro->create();
  14.                 $aro->save(array(
  15. 'alias' => $this->data['User']['username'],
  16. 'model' => 'User',
  17. 'foreign_key' => $this->User->id,
  18. 'parent_id' => $parent['Aro']['id'])
  19. );
  20. $this->Acl->Aro->save();
  21. $this->redirect(array('action' => 'index'), null, true);
  22. } else {
  23. $this->data['User']['password'] = '';
  24. $this->Session->setFlash('There was a problem saving
  25.                                   this information');
  26. }
  27. }
  28.     }
  29.                
复制代码
回复

使用道具 举报

 楼主| 发表于 27-2-2010 10:33 AM | 显示全部楼层
本帖最后由 雷洛 于 27-2-2010 10:37 AM 编辑

PRODUCT的權限assign (ACO)
  1.                     
  2.                     
  3.      function add() {
  4.         if (!empty($this->data)) {
  5.             $this->Product->create();
  6.             if ($this->Product->save($this->data)) {
  7.                 $dealer = $this->Product->Dealer->read(null,
  8. $this->data['Product']['dealer_id']);
  9.                 $parent = $this->Acl->Aco->findByAlias(
  10. $dealer['Dealer']['title']);
  11.                 $alias = $this->Product->id.'-'.$this->data
  12. ['Product']['title'];
  13.                 $aco = new Aco();
  14.                 $aco->create();  
  15.                 $aco->save(array(
  16.                     'alias' => $alias,                                                
  17.                     'model' => 'Product',
  18.                     'foreign_key' => $this->Product->id,
  19.                     'parent_id' => $parent['Aco']['id']
  20.                 ));
  21.                 $this->Acl->allow('Users', $alias, 'read');
  22.                 $this->Acl->allow($this->Session->read('user'), $alias);
  23.                 $this->Session->setFlash(__('The Product has been saved', true));
  24.                 $this->redirect(array('action'=>'index'));
  25.             } else {
  26.                 $this->Session->setFlash(__('The Product could not be saved.
  27. Please, try again.', true));
  28.             }
  29.         }
  30.         $dealers = $this->Product->Dealer->find('list');
  31.         $this->set(compact('dealers'));
  32.     }
  33.                
  34.                
复制代码
最後的權限實現 檢查VIEW
  1. function view($id = null) {if (!$id) {
  2.   $this->Session->setFlash('Invalid Product.', true));
  3.   $this->redirect(array('action'=>'index'));
  4. }
  5. $product = $this->Product->read(null, $id);
  6. $alias = $id . '-' . $product['Product']['title'];
  7. if ($this->Acl->check($this->Session->read('user'), $alias;
  8.                             $id . '-' . $product['Product']['title'], 'read')) {
  9. $this->set('product', $product);
  10. } else {
  11.    $this->Session->setFlash('Only registered users may view this product.');
  12.    $this->redirect(array('action'=>'index'));
  13.        }
  14. }
  15.          
复制代码
EDIT
  1.   function edit($id = null) {  
  2.         if (!$id && empty($this->data)) {
  3.             $this->Session->setFlash(__('Invalid Product', true));
  4.             $this->redirect(array('action'=>'index'));
  5.         }
  6.         $product = $this->Product->read(null, $id);
  7.         $alias = $id.'-'.$product['Product']['title'];
  8.         if ($this->Acl->check($this->Session->read('user'),
  9. $alias, 'update')) {
  10.             if (!empty($this->data)) {
  11.                 if ($this->Product->save($this->data)) {
  12.                     $this->Session->setFlash(__('The Product has been saved',
  13. true));
  14.                     $this->redirect(array('action'=>'index'));
  15.                 } else {
  16.                     $this->Session->setFlash(__('The Product could not be saved.
  17. Please, try again.', true));
  18.                 }
  19.             }
  20.             if (empty($this->data)) {
  21.                 $this->data = $this->Product->read(null, $id);
  22.             }
  23.         } else {
  24.             $this->Session->setFlash('You cannot edit this product.');
  25.             $this->redirect(array('action'=>'index'), null, true);
  26.         }
  27.         $dealers = $this->Product->Dealer->find('list');
  28.         $this->set(compact('dealers'));
  29.     }

复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 29-11-2025 03:00 AM , Processed in 0.132818 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表