Difference between #7 and #8 of
RBAC Super Simple with Admin and User

Revision #8 has been created by evercode on Jan 13, 2015, 10:23:19 PM with the memo:

Added change on user table to reflect Yii 2.0.2 change. Added more instructions for clarity.
« previous (#7)

Changes

Title unchanged

RBAC Super Simple with Admin and User

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

rbac, Yii, matchCallback, roles, behaviors, access rules, advanced template

Content changed

One of the common requests I see in the forum is how to implement RBAC. While you can implement Yii 2's built-in RBAC, that might be too much for developers who are just starting with Yii 2 or have simpler needs. Sometimes you are looking for a fast solution and just want two flavors, user and admin. And even if you will eventually need more, you can use these methods as a starting point for developing your own features or move on to Yii 2's RBAC. So this is a variation on my own implementation which is more involved, but this will get you the basics quickly. Using Yii 2's advanced template, 99.9 % of the work is done before you start. So here is what we'll do: 1. add constants to the User model for admin and user role & add role column on user table, type int, not null, default value 10. 2. add the contstants into the range of values for user roles
3. create a static method on the User model to check isUserAdmin
4. create a loginAdmin method on the LoginForm model
[...]
We use the constant to set the value of admin to 20. We are going to use the Role value on the user record to compare to this number. So, if the user's role is set to 20, they are admin. The default on signup is 10, so the only way for a user to get 20 on role at this point is for you to assign it directly in the DB (probably through PhpMyAdmin).

Since I wrote this tutorial orginally, Yii 2 dropped the role column from the advanced application in it's out-of-the-box build, so you will have to add a role column to your user table manually before continuing on.
  Make the role column int, not null, and default value = 10.

**Step 2**
[...]
```

We
add default rule and simply limit the allowed range of values for role to the two that we have defined in our constants.

**Step 3**

Add the following method at the bottom of the User model:
[...]
9 1
15 followers
Viewed: 101 333 times
Version: 2.0
Category: Tutorials
Written by: evercode
Last updated by: evercode
Created on: Nov 9, 2014
Last updated: 9 years ago
Update Article

Revisions

View all history