Wiki

Clone wiki

cruge-english / Cruge tutorial

Cruge uses the Yii built-in concepts of RBAC (Role Based Access Management) represented by Roles, Tasks and Operations. A user can have Roles, Tasks and Operations assigned to them.

you can check if the current user has access by using code as

#!php
<?php
if (Yii::app()->user->checkAccess("action_site_index")){
    //...your code here...
}
?>

List of API's available in Cruge:

#!php
<?php
Yii::app()->user
    /* To access the very basic functions of the user control. 100% compatible with Standard Yii. */
?>
#!php
<?php
Yii::app()->user->ui
    /*  Provides links to interface functions. */
?>
#!php
<?php
Yii::app()->user->um
    /*   Provides access to user management */
?>
#!php
<?php
Yii::app()->user->rbac
    /*   Provides access to the features of RBAC. */
?>

A little hint - this code snippet you can get more info about what you can do with the objects above, just place it in a controller action and it will give you a list of all methods for the given object.

#!php 
<?php
$object = Yii::app()->user->ui;
echo "<pre>";
$methods = get_class_methods(get_class($object));
print_r($methods);
echo "</pre>";
exit;
?>

OR you an look up code in protected/modules/cruge/components.

Now back on using Cruge. Let's imagine your online bookshop has a Favourite Books List function. The user should be able to add and remove his favourite books. The link should let your user delete a favourite book.

#!html
"Your Favorite <a href='index.php?r=favoriteBooks/eliminate&id=123'> Delete Selected </a>" 
This should delete the right row in the database that coresponds to the user's favorite book. But what if someone tries to delete his favorite book. Let's say another user types in http://index.php?r=favoriteBooks/eliminate&id=123 ? Here is where business rules come in handy You can build these in your controller and use the method to verify user
#!php
<?php
public function actionDeleteFavorite($favoriteIdentifier) {
    $bookmark = Bookmark::model()->findByPk ($favoriteIdentifier);
    if ($bookmark->created_by_user_id == Yii::app()->user->id) {
       $bookmark->delete();
       return;
    }
    //or if the created_by use
    throw new CHttpException(500, "Sorry you can not delete the bookmark of another user.");
}
?>


Examples of Cruge API

To use the api users should be logged in like

#!php
<?php
Yii::app()->user->um

To get the active user id

#!php
<?php
$id = Yii::app()->user->id; // standard Yii

If user is guest

#!php
<?php
$booleanResult = Yii::app()->user->isGuest; // standard Yii

If user is super administrator

#!php
<?php
$booleanResult = Yii::app()->user->isSuperAdmin; // NOT standard Yii, but a Cruge vaue

Get active user's email

#!php
<?php
$email = Yii::app()->user->email;

Check if the user has access

#!php
<?php
if(Yii::app()->user->checkAccess('xxx')) {...}
//where xxx is the name of the Role, Task or Operation.

Load an user by ID

#!php
<?php
$user = Yii::app()->user->um->loadUserById(123 /*, true (to load their fields)*/);

Load an user by username

#!php?
<?php
  / / Looking strictly for your username, unlike LoadUser () who are looking for email or username

$user = Yii::app()->user->um->loadUserByUsername('jsmith' /*, true (to load their fields)*/);

Load an user by username or email

#!php
<?php
  / / Looking strictly for your username, unlike LoadUser () who are looking for email or username

$user = Yii::app()->user->um->loadUser('admin@this-site.com' /*, true (to load their fields)*/);

Load user by a custom field value

#!php
<?php
$user = Yii::app()->user->um->loadUserByCustomField('something', $something); /

Get all users

#!php
<?php
Yii::app()->user->um->listusers()


Google Translated Cruge Spanish Tutorial

Updated