00001 <?php
00022 class SGlobalUser extends SUser {
00023
00024 protected $id;
00025 protected $userName;
00026 protected $firstName;
00027 protected $lastName;
00028 protected $role = SUser::ANONYMOUS;
00029 protected $flash = array();
00030 protected $sessionData = array();
00031 protected $authRealms = array();
00032
00033 protected $config = null;
00034
00035
00036
00037
00038
00039 public function __construct() {
00040 parent::__construct();
00041
00042
00043 }
00044
00049 public function doc() {
00050 return "An instance of this class represents the global Shodor user"
00051 . " that follows you everywhere, with the point of providing authentication.";
00052 }
00053
00057 public function getAttributes() {
00058 return array('id', 'userName', 'firstName', 'lastName', 'role');
00059 }
00060
00064 public function getUniqueAttributes() {
00065 return array('id', 'userName');
00066 }
00067
00072 public function populate() {
00073 return false;
00074 }
00075
00080 public function commit() {
00081 return false;
00082 }
00083
00084
00085
00086
00087
00093 public function getFirstName() {
00094 if(count($this->authRealms) > 0) {
00095 reset($this->authRealms);
00096 return current($this->authRealms)->getFirstName();
00097 }
00098 else {
00099 return $this->firstName;
00100 }
00101 }
00102
00108 public function getLastName() {
00109 if(count($this->authRealms) > 0) {
00110 reset($this->authRealms);
00111 return current($this->authRealms)->getLastName();
00112 }
00113 else {
00114 return $this->lastName;
00115 }
00116 }
00117
00123 public function getEmail() {
00124 if(count($this->authRealms) > 0) {
00125 reset($this->authRealms);
00126 return current($this->authRealms)->getEmail();
00127 }
00128 }
00129
00135 public function getRole() {
00136 if(count($this->authRealms) > 0) {
00137 reset($this->authRealms);
00138 return current($this->authRealms)->getRole();
00139 }
00140 else {
00141 return $this->role;
00142 }
00143 }
00144
00145
00146
00147
00148
00159 public function isAuthenticatedFor($realm){
00160 if(isset($this->authRealms[$realm])) {
00161 return true;
00162 } else {
00163 return false;
00164 }
00165 }
00166
00175 public function isAuthenticated() {
00176 return count($this->authRealms);
00177 }
00178
00192 public function authenticateWith($realm, $username, $password) {
00193 # If the user already is authenticated in the requested realm
00194 # (i.e., he/she has the realm's auth module in this object)
00195 # then return true
00196 if(isset($this->authRealms[$realm])) {
00197 return true;
00198 }
00199
00200 # Otherwise, try to authenticate using the auth module
00201 # Make a new instance of the realm class
00202 $realmClass = $this->getRealmClassName($realm);
00203 $mod = new $realmClass();
00204
00205 $result = $mod->authenticate($username, $password);
00206
00207 #Authenticate with the realm class
00208 if($result) {
00209 # If authentication works, store the auth module locally
00210 # for later checks/fetching of data.
00211 $this->addAuthRealm($realm, $mod, $username);
00212 $this->setUserName($username);
00213 return true;
00214 }
00215
00216 # If authentication fails, set a warning and return false
00217 $this->setPrettyWarning('authenticateWith', "Auth failed for user $username in realm $realm");
00218 return false;
00219 }
00220
00229 public function deAuthenticateFor($realm) {
00230 unset($this->authRealms[$realm]);
00231 }
00232
00238 public function deAuthenticateAll() {
00239 $this->authRealms = array();
00240 }
00241
00242 # getRealmClassName
00243 # all trusted auth modules should be named S___AuthModule
00244 # --------------------------------------------------------
00245
00254 public function getRealmClassName($realm) {
00255 return "S" . ucfirst(strtolower($realm)) . "AuthModule";
00256 }
00257
00258 # addAuthRealm
00259 # add an auth module to our array of auth realms
00260 # --------------------------------------------------------
00261
00270 protected function addAuthRealm($realm, $mod, $username) {
00271 $this->authRealms[$realm] = $mod;
00272 }
00273
00274 # getAuthModule
00275 # fetch an auth module from the array
00276 # --------------------------------------------------------
00277
00286 public function getAuthModule($name) {
00287 if(isset($this->authRealms[$name]))
00288 return $this->authRealms[$name];
00289 else
00290 return null;
00291 }
00292 }
00293
00294 ?>