2011-06-05 50 views
0

我尝试登录时显示的用户名和用户角色成功
CakePHP的问题,当登录成功后

这个我按照net.tusplus例如

class AccessComponent extends Object{ 
var $components = array('Acl', 'Auth'); 
var $user; 

function startup(){ 
    $this->user = $this->Auth->user(); 
} 

function check($aco, $action='*'){ 
    if(!empty($this->user) && $this->Acl->check('Group::'.$this->user['Group']['id'], $aco, $action)){ 
     return true; 
    } else { 
     return false; 
    } 
} 

function checkHelper($aro, $aco, $action = "*"){ 
    App::import('Component', 'Acl'); 
    $acl = new AclComponent(); 
    return $acl->check($aro, $aco, $action); 
} 
} 


class AccessHelper extends Helper { 

var $helpers = array("Session"); 
var $Access; 
var $Auth; 
var $user; 

function beforeRender() { 
    App::import('Component', 'Access'); 
    $this->Access = new AccessComponent(); 

    App::import('Component', 'Auth'); 
    $this->Auth = new AuthComponent(); 
    $this->Auth->Session = $this->Session; 

    $this->user = $this->Auth->user(); 
} 

function check($aco, $action='*') { 
    if (empty($this->user)) 
     return false; 
    return $this->Access->checkHelper('Group::' . $this->user['Users']['id'], $aco, $action); 
} 

function isLoggedin() { 
    return!empty($this->user); 
} 

我尝试在显示状态查看页面

<div id="status"> 
<?php 
if ($access->isLoggedin()) { 
    echo "Welcome"; 

    echo $this->Html->link('Sign Out', array('controller' => 'users', 'action' => 'logout')); 
} else { 
    echo $this->Html->link('Sign In', array('controller' => 'users', 'action' => 'login')); 
    echo"</br>"; 
    echo $this->Html->link('Sign Up', array('controller' => 'users', 'action' => 'register')); 
} 
?> 

如果我想显示组名和用户名。我应该怎么做

回答

1

您可以将您在启动功能中设置的用户变量传递给您的视图并显示其中的信息。

控制器

$this->set('authUser', $this->user); 

视图

<div id="welcome"> 
    Welcome, <?php echo $authUser['User']['username'];?> 
</div>