+ // Specialization of the buildPerms method
+ // This function build 'generic' permissions for the user. It does not take
+ // into account page specific permissions (e.g X.net group permissions)
+ protected function buildPerms()
+ {
+ if (!is_null($this->perm_flags)) {
+ return;
+ }
+ if ($this->perms === null) {
+ $this->loadMainFields();
+ }
+ $this->perm_flags = self::makePerms($this->perms, $this->is_admin);
+ }
+
+ // We do not want to store the password in the object.
+ // So, fetch it 'on demand'
+ public function password()
+ {
+ return XDB::fetchOneCell('SELECT a.password
+ FROM accounts AS a
+ WHERE a.uid = {?}', $this->id());
+ }
+
+ /** Overload PlUser::promo(): there no promo defined for a user in the current
+ * schema. The promo is a field from the profile.
+ */
+ public function promo()
+ {
+ if (!$this->hasProfile()) {
+ return '';
+ }
+ return $this->profile()->promo();
+ }
+
+ /** Return the main profile attached with this account if any.
+ */
+ public function profile()
+ {
+ if (!$this->_profile_fetched) {
+ $this->_profile_fetched = true;
+ $this->_profile = Profile::get($this);
+ }
+ return $this->_profile;
+ }
+
+ /** Return true if the user has an associated profile.
+ */
+ public function hasProfile()
+ {
+ return !is_null($this->profile());
+ }
+
+ /** Check if the user can edit to given profile.
+ */
+ public function canEdit(Profile $profile)
+ {
+ // XXX: Check permissions (e.g. secretary permission)
+ // and flags from the profile
+ return XDB::fetchOneCell('SELECT pid
+ FROM account_profiles
+ WHERE uid = {?} AND pid = {?}',
+ $this->id(), $profile->id());
+ }
+
+ /** Get the email alias of the user.
+ */
+ public function emailAlias()
+ {
+ global $globals;
+ $data = $this->emailAliases($globals->mail->alias_dom);
+ if (count($data) > 0) {
+ return array_pop($data);
+ }
+ return null;
+ }
+
+ /** Get all the aliases the user belongs to.
+ */
+ public function emailAliases($domain = null)
+ {
+ $where = '';
+ if (!is_null($domain)) {
+ $where = XDB::format(' AND alias LIKE CONCAT("%@", {?})', $domain);
+ }
+ return XDB::fetchColumn('SELECT v.alias
+ FROM virtual AS v
+ INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid)
+ WHERE (vr.redirect = {?} OR vr.redirect = {?})
+ ' . $where,
+ $this->forlifeEmail(), $this->m4xForlifeEmail());
+ }
+
+ /** Get the alternative forlife email
+ * TODO: remove this uber-ugly hack. The issue is that you need to remove
+ * all @m4x.org addresses in virtual_redirect first.
+ * XXX: This is juste to make code more readable, to be remove as soon as possible
+ */
+ public function m4xForlifeEmail()
+ {
+ global $globals;
+ trigger_error('USING M4X FORLIFE', E_USER_NOTICE);
+ return $this->login() . '@' . $globals->mail->domain2;
+ }
+
+ // Return permission flags for a given permission level.
+ public static function makePerms($perms, $is_admin)
+ {
+ $flags = new PlFlagSet($perms);
+ $flags->addFlag(PERMS_USER);
+ if ($is_admin) {
+ $flags->addFlag(PERMS_ADMIN);
+ }
+ return $flags;
+ }
+