4beee0a3a183e17a914fe18818980f90673eabbe
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // PlUserNotFound is raised when a given id cannot be linked to an existing user.
23 // The @p results give the list hruids (useful when several users are found).
24 class UserNotFoundException
extends Exception
26 public function __construct($results = array())
28 $this->results
= $results;
29 parent
::__construct();
33 // Represents an user of the system, with a special focus on its identification
34 // (hruid, forlife, and bestalias).
35 // Note: each implementation of platal-core MUST create a subclass 'User' of
36 // this abstract PlUser class.
39 // User's data storage. By convention, null means the information hasn't
40 // been fetched yet, and false means the information is not available.
42 // Main (unique) identifiers.
43 protected $user_id = null
;
44 protected $hruid = null
;
46 // User's emails (bestalias should be preferred when sending emails).
47 protected $forlife = null
;
48 protected $bestalias = null
;
51 // Constructs the object from an identifier (hruid/uid/email alias/email
52 // redirection) and an optionnal array of known user properties.
53 public function __construct($login, $values = array())
55 $this->fillFromArray($values);
56 if (!isset($this->user_id
) ||
!isset($this->hruid
)) {
57 list($this->user_id
, $this->hruid
) = $this->getLogin($login);
62 // Properties accessors.
63 public function id() { return $this->user_id
; }
64 public function login() { return $this->hruid
; }
65 abstract public function bestEmail();
66 abstract public function forlifeEmail();
69 // Determines if the @p id is a valid identifier; if so, returns the user_id
70 // and the hruid. Otherwise raises UserNotFoundException.
71 abstract protected function getLogin($login);
73 // Fills the object from associative arrays containing our data.
74 // The use case is for arrays got directly from anoter SQL request.
75 protected function fillFromArray(array $values)
77 // It might happen that the 'user_id' field is called uid in some places
78 // (eg. in sessions), so we hard link uid to user_id to prevent useless
80 if (!isset($values['user_id']) && isset($values['uid'])) {
81 $values['user_id'] = $values['uid'];
84 foreach ($values as $key => $value) {
85 if (property_exists($this, $key) && !isset($this->$key)) {
92 // Returns a valid User object built from the @p id and optionnal @p values,
93 // or returns false and calls the callback if the @p id is not valid.
94 public static function get($login, $callback = false
)
96 return User
::getWithValues($login, array(), $callback);
99 public static function getWithValues($login, $values, $callback = false
)
102 $callback = array(__CLASS__
, '_default_user_callback');
106 return new User($login, $values);
107 } catch (UserNotFoundException
$e) {
108 return call_user_func($callback, $login, $e->results
);
112 // Alias on get() with the silent callback.
113 public static function getSilent($login)
115 return User
::getWithValues($login, array(), array(__CLASS__
, '_silent_user_callback'));
118 // Returns the forlife emails for @p members. If @p strict mode is enabled,
119 // it returns the list of validated forlife emails. If strict mode is not,
120 // it also returns unvalidated values (but still call the callback for them).
121 public static function getBulkForlifeEmails($logins, $strict = true
, $callback = false
)
123 if (!is_array($logins)) {
124 if (strlen(trim($logins)) == 0) {
127 $logins = explode(' ', $logins);
132 foreach ($logins as $i => $login) {
133 if (($user = User
::get($login, $callback))) {
134 $list[$i] = $user->forlifeEmail();
135 } else if(!$strict) {
144 // Silent callback for the user lookup -- does nothing.
145 public static function _silent_user_callback($login, $results)
150 // Default callback for user lookup -- displays an error message w.r.t. the
151 // number of matching users found.
152 abstract public static function _default_user_callback($login, $results);
155 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: