ldap->search_uid($uid); if (!$ldapentry) { # Create fake entry, the fullname equals uid and no additional groups $ldapentry['cn'][0] = $uid; $ldapentry['displayname'][0] = $uid; $ldapentry['ou;lang-en']['count'] = 0; } if(is_null($name)) $name = $ldapentry['cn'][0]; # FIXME: misschien displayname ipv cn? # Create a 'personal' group as well $newgroup = \App\Model\UserGroup::create($database, $uid.' (myself)'); $activerow = $database->context->table('lusers')->insert(['name' => $uid, 'description' => $name, 'primarygroup' => $newgroup->primary]); $user = $database->createModel('User', $activerow); $user->addGroup($newgroup); # Add/create additional groups for ($i=0; $i<$ldapentry['ou;lang-en']['count']; $i++) { $additionalgroup = \App\Model\UserGroup::create($database, $ldapentry["ou;lang-en"][$i]); $user->addGroup($additionalgroup); } return $user; } public function getUid() { return $this->activerow->name; } public function setUid($uid) { $this->activerow->update(['name' => $uid]); } public function getName() { return $this->activerow->description; } public function setName($name) { $this->activerow->update(['description' => $name]); } public function isAdmin() { return $this->activerow->admin; } public function isModerator() { return $this->isAdmin(); } public function getGroups() { $rows = $this->context->table('lusergroups') ->where(':lusergroupmembers.luser', $this->activerow->luser)->order('lusergroups.name'); $groups = []; foreach($rows as $row) { $e = $this->database->createModel('UserGroup', $row); $groups[$e->primary] = $e; } return $groups; } public function inGroup($groupid) { # groupid can be either groupid or groupname $mygroups = $this->getGroups(); foreach($mygroups as $mygroup) { if ($mygroup->primary == $groupid) { return true; } if ($mygroup->name == $groupid) { return true; } } return false; } public function setGroups($groups) { throw new \Exception("setGroups() not implemented yet"); } public function addGroup($group, $groupadmin=false) { $result = $this->context->table('lusergroupmembers')->insert([ 'luser' => $this->primary, 'lusergroup' => $group->primary, 'admin' => $groupadmin ]); return $result; } public function getPrimarygroup() { $row = $this->activerow->ref('lusergroups', 'primarygroup'); return $this->database->createModel('UserGroup', $row); } public function getAddresslists() { $rows = $this->context->table('addresslists') ->where('lusergroup:lusergroupmembers.luser', $this->activerow->luser); $addresslists = []; $database = $this->database; foreach($rows as $row) { $addresslist = $database->createModel('Addresslist', $row); $addresslists[$addresslist->primary] = $addresslist; } return $addresslists; } public function get_addresslist($id) { return $this->addresslists[$id]; } public function setAddresslists($addresslists) { throw "setAddresslists() not implemented yet"; $newaddresslists = []; foreach($addresslists as $addresslist) $newaddresslists[$addresslist->primary] = addresslist; $oldaddresslists = []; foreach($this->getAddresslists() as $addresslist) { if(array_key_exists($addresslist->primary, $newaddresslists)) $oldaddresslists[$addresslist->primary] = addresslist; else $addresslist->delete(); } foreach($newaddresslists as $id => $addresslist) if(!array_key_exists($addresslist->id, $oldaddresslists)) $this->dummy_create_and_insert_addresslist($addresslist); } private function wrap_mailings($rows) { $mailings = []; $database = $this->database; foreach($rows as $row) { $mailing = $database->createModel('Mailing', $row); $mailings[$mailing->primary] = $mailing; } return $this->mailings = $mailings; } public function getMailings() { if($this->mailings) return $this->mailings; #$rows = $this->activerow->related('lusergroupmembers')->where('mailings.lusergroup = lusergroupmembers.lusergroup')->order('ctime'); $rows = $this->context->table('mailings')->where('lusergroup:lusergroupmembers.luser', $this->primary)->order('ctime'); return $this->wrap_mailings($rows); } public function getApproval_requests() { if($this->approval_requests) return $this->approval_requests; if(!$this->isAdmin()) return []; $rows = $this->context->table('mailings') ->where("submitted_when > mtime AND COALESCE(approved_when, '1970-01-01'::TIMESTAMP) < mtime") ->order('ctime'); return $this->wrap_mailings($rows); } public function setMailings($mailings) { throw new \Exception("setMailings() not implemented yet"); } public function get_mailing($mailingid) { return $this->getMailings()[$mailingid]; } /* Functions which return data from external sources like LDAP */ private function getLdap_entry() { /* Interesting entries for us: * cn, displayName, mail, uid, uvt-lau, ou;lang-en(multi) */ if($this->ldap_entry) return $this->ldap_entry; return $this->ldap_entry = $this->database->ldap->search_uid($this->getUid()); } public function getEmailaddress() { if($this->emailaddress) return $this->emailaddress; $entry = $this->getLdap_entry(); if (array_key_exists('mail', $entry) && is_array($entry['mail']) && array_key_exists(0, $entry['mail'])) { $this->emailaddress = $entry['mail'][0]; } return $this->emailaddress; } public function getDefault_mailing() { return $this->database->createModel('DefaultMailing', $this); } }