1 <?php
2 3 4 5 6 7
8 class WebUserBehavior extends CBehavior
9 {
10 public function getRole()
11 {
12 return $this->owner->getState('__role');
13 }
14
15 public function getId()
16 {
17 return $this->owner->getState('__id') ? $this->owner->getState('__id') : 0;
18 }
19
20 protected function afterLogin($fromCookie)
21 {
22 parent::afterLogin($fromCookie);
23 $this->updateSession();
24 }
25
26 public function updateSession()
27 {
28 $user = Yii::app()->getModule('user')->user($this->id);
29
30 if ($user == false) return;
31
32 $this->owner->name = $user->username;
33 $userAttributes = CMap::mergeArray(array(
34 'email' => $user->email,
35 'username' => $user->username,
36 'create_at' => $user->create_at,
37 'lastvisit_at' => $user->lastvisit_at,
38 ), $user->profile->getAttributes());
39 foreach ($userAttributes as $attrName => $attrValue) {
40 $this->owner->setState($attrName, $attrValue);
41 }
42 }
43
44 public function model($id = 0)
45 {
46 return Yii::app()->getModule('user')->user($id);
47 }
48
49 public function user($id = 0)
50 {
51 return $this->owner->model($id);
52 }
53
54 public function getUserByName($username)
55 {
56 return Yii::app()->getModule('user')->getUserByName($username);
57 }
58
59 public function getAdmins()
60 {
61 return Yii::app()->getModule('user')->getAdmins();
62 }
63
64 public function isAdmin()
65 {
66 return Yii::app()->getModule('user')->isAdmin();
67 }
68 }
69