The method of Yii permission control (three kinds of methods) _php instance

Source: Internet
Author: User
Tags yii

This article illustrates the method of Yii permission control. Share to everyone for your reference, specific as follows:

Here are excerpts of the following 3:

1. Through AccessControl:

Public function Filters ()
{return
  array (
    ' AccessControl ',//Perform access control for CRUD operations
  );
}
/**
 * Specifies the access control rules.
 * This are used by the ' AccessControl ' filter.
 * @return Array access control rules/public
function accessrules ()
{return
  array (
    ' Allow ',//Allow authenticated users to access all actions
      ' users ' =>array (' @ '),
    Array (' Deny ',// Deny all users '
      users ' =>array (' * '),)
  ;
}

2. Through the plugin (such as: right)

Public function Filters ()
{return
  array (
    ' rights ',
  );
}

3. Blending mode:

/**
 * @return Array Action Filters
 *
/Public Function filters ()
{return
  array (
    ' Updateown + Update ',//Apply This filter to the update action.
    ' Rights ',
  );
}
/**
 * Filter to checking whether the currently logged in user * is the author of the
 post being accessed .
 * * Public
function Filterupdateown ($filterChain)
{
  $post = $this->loadmodel ();
  Remove the ' Rights ' filter if the user is updating a own post/and has the permission to does so
  .
  if (Yii::app ()->user->checkaccess (' Postupdateown ', Array (' userid ' => $post->author_id))
    $ Filterchain->removeat (1);
  $filterChain->run ();
}

If you have permission to open certain actions on the basis of permission, you can pass allowedactions:

Public Function allowedactions ()
{return
  ' Autocomplate,autocomplate2 ';
}

I hope this article will help you with the PHP program design based on the YII framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.