tags:

views:

21

answers:

1

Hi I am relatively new to cakephp. I try to create a simple app as learning&practice. I implemented a simple user handling system using Auth component. I made a custom isAuthorized method in the user model. After registration the user need to activate himself/herself in order to be able to login. If the user is not active, is Authorized gives back a false and also I want to inform the user that he isn't activated. But I do not know how to set the flash message inside this method (I tried without a success).

function isAuthorized($user, $controller, $action) { 
 // We have simply user-rights system. There are admins and non-admins.
 // This function makes a decision if the user have rights to do what requested.
  if($user['User']['active']==0) { // if the user has not been activated yet then gets no rights
   $authed=false;
   $this->Auth->authError = __("Please activate your user first!",true);
  } else {
   $this->Auth->authError = __("Sorry, you do not have access to this area.",true);
   if($user['User']['admin']==1) { //all admins has all rights
    $authed=true;
   } else {
    if($controller=='Areas' or $controller=='Rooms' or $controller=='Categories') {
     if($action=='read') {
      $authed=true;
     } else {
      $authed=false; //non-admin users are not authorized to add or edit rooms or areas
     }
    } else {
     $authed=true; // however they are allowed to do other tasks. the per task rights are controlled within the controllers
    }
   }
  }
  //debug($authed);
  return $authed;

As you see I tried, but I failed :) it does not work.

I also tried to put

$this->Auth->authError = __("Please activate your user first!",true);

in users controller login() without success

A: 

I realized that the Auth component is activated sooner then the controller method and takes action if the user is unauthorized what means it redirects before the controller has a chance to modify the authError. That's why I get my "standard" authError.

As I use a custom authorization function (in the model) the user is authenticated already (if has an entry) and the Auth part of the session variables has been set. So if I checking the session auth datas I can decide in the controller if the user is authed and not activated. Then I set a flash message so the user is informed by an auth error message from the auth component (do not have access to that area) and another message as normal flash message to activate him/herself.

function login() {
    if(empty($this->Auth->data) and $this->Session->read('Auth.User.active')===0){
        $this->Session->setFlash(__('Please activate your user first.',true));
    }
}

I suppose it is not the most elegant way to solve this ;) but I am new to web programming and also cakephp.

sipiatti