/** * Is the user in a group. * @param string|\vakata\user\GroupInterface $group the group to check for * @return boolean is the user in the group */ public function inGroup($group) : bool { $id = $group instanceof GroupInterface ? $group->getID() : $group; return isset($this->groups[$id]); }
/** * Remove a user form a group * @param vakata\user\GroupInterface $group the group to remove the user from * @return self */ public function deleteGroup(GroupInterface $group) : UserInterface { if (isset($this->groups[$group->getID()])) { unset($this->groups[$group->getID()]); if ($this->primary->getID() === $group->getID()) { $this->primary = null; } } return $this; }
/** * Save a group. * @param \vakata\user\GroupInterface $group the group to save * @return self */ public function saveGroup(GroupInterface $group) : UserManagementInterface { $trans = $this->db->begin(); try { if (!$this->db->one("SELECT 1 FROM " . $this->options['tableGroups'] . " WHERE grp = ?", [$group->getID()])) { $this->db->query("INSERT INTO " . $this->options['tableGroups'] . " (grp, created) VALUES (?, ?)", [$group->getID(), date('Y-m-d H:i:s')]); } foreach ($group->getPermissions() as $permission) { if (!$this->db->one("SELECT 1 FROM " . $this->options['tablePermissions'] . " WHERE perm = ?", [$permission])) { $this->db->query("INSERT INTO " . $this->options['tablePermissions'] . " (perm, created) VALUES (?, ?)\n ON DUPLICATE KEY UPDATE perm = perm", [$permission, date('Y-m-d H:i:s')]); } if (!$this->db->one("SELECT 1 FROM " . $this->options['tableGroupsPermissions'] . " WHERE grp = ? AND perm = ?", [$group->getID(), $permission])) { $this->db->query("INSERT INTO " . $this->options['tableGroupsPermissions'] . " (grp, perm, created) VALUES (?, ?, ?)", [$group->getID(), $permission, date('Y-m-d H:i:s')]); } } $this->db->commit(); parent::saveGroup($group); return $this; } catch (\Exception $e) { $this->db->rollback($trans); throw $e; } }