-
-
Save Flatroy/302cf74d645b44e8a699 to your computer and use it in GitHub Desktop.
Kohana 3.X auth snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //Useful code snippets | |
| //Here are some code snippets which show you the basics of Auth: | |
| //Create a new user (e.g. if you have not set up any users yet and want to do that programmatically) | |
| $model = ORM::factory('user'); | |
| $model->values(array( | |
| 'username' => 'admin', | |
| 'email' => '[email protected]', | |
| 'password' => 'admin', | |
| 'password_confirm' => 'admin', | |
| )); | |
| $model->save(); | |
| // remember to add the login role AND the admin role | |
| // add a role; add() executes the query immediately | |
| $model->add('roles', ORM::factory('role')->where('name', '=', 'login')->find()); | |
| $model->add('roles', ORM::factory('role')->where('name', '=', 'admin')->find()); | |
| //Check whether current user has a role | |
| Auth::instance()->logged_in('admin') // for current user | |
| //Check whether some other user has a role | |
| // Load the role | |
| $role = ORM::factory('role', array('name' => 'admin')); | |
| // Check that the user has the given role | |
| $status = $user->has('roles', $role); | |
| //Add a role to a user | |
| // add() executes the query immediately, and saves the data (unlike the KO2 docs say) | |
| $user->add('roles', ORM::factory('role')->where('name', '=', 'admin')->find()); | |
| //Remove a role from a user | |
| // remove() executes the query immediately | |
| $user->remove('roles', ORM::factory('role')->where('name', '=', 'admin')->find()); | |
| //Retrieve all users that have a particular role | |
| // find all the reviewer users in order of their username | |
| $role = ORM::factory('role')->where('name', '=', 'reviewer')->find(); | |
| $users = $role->users->order_by('username', 'DESC')->find_all(); | |
| foreach($users as $user) {...} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment