summaryrefslogtreecommitdiffstats
path: root/app/class/agent.class.php
diff options
context:
space:
mode:
authorMalf Furious <m@lfurio.us>2018-10-27 18:28:22 -0400
committerMalf Furious <m@lfurio.us>2018-10-27 18:28:22 -0400
commit0421aa1b60f4fe6bf140888159c58059c1013588 (patch)
treec3285276f6c53b6789e2f6dc82cb3b0fd17b38a4 /app/class/agent.class.php
parent495157341d60522084dcc9f6219877b6ba497312 (diff)
parent6512655aee73d3d295daa4de0e4ef25c08cfec9e (diff)
downloadscrott-0421aa1b60f4fe6bf140888159c58059c1013588.tar.gz
scrott-0421aa1b60f4fe6bf140888159c58059c1013588.zip
Merge branch 'rel/v0.1'v0.1
Diffstat (limited to 'app/class/agent.class.php')
-rw-r--r--app/class/agent.class.php458
1 files changed, 458 insertions, 0 deletions
diff --git a/app/class/agent.class.php b/app/class/agent.class.php
new file mode 100644
index 0000000..4c75f0b
--- /dev/null
+++ b/app/class/agent.class.php
@@ -0,0 +1,458 @@
+<?php
+
+/*
+ * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * For more information, please refer to UNLICENSE
+ */
+
+require_once "class/obj.class.php";
+require_once "class/pad.class.php";
+require_once "class/settings.class.php";
+require_once "class/phpmailer.class.php";
+require_once "class/smtp.class.php";
+
+/*
+ * This is a supertype for users and groups, since these two object types
+ * will often be handled polymorphically and will share some functionality.
+ */
+abstract class agent extends obj
+{
+ /*
+ * Constructor
+ */
+ public function __construct(?string $guid = NULL)
+ {
+ parent::__construct($guid);
+ }
+
+ /*
+ * Since this class is abstract, this function is provided as a
+ * means of constructing the appropriate agent object based on
+ * a GUID.
+ */
+ public static function getAgentObj(string $guid) : agent
+ {
+ try
+ {
+ return new user($guid);
+ }
+ catch (Exception $e)
+ {
+ return new group($guid);
+ }
+ }
+
+ /*
+ * Check whether this agent is the owner of the given object
+ */
+ public function isOwnerOf(obj $obj) : bool
+ {
+ if (!($own = $obj->getOwner()))
+ return false;
+
+ return $own->guid == $this->guid;
+ }
+
+ /*
+ * Check whether this agent is a member of the given object
+ */
+ public function isMemberOf(obj $obj) : bool
+ {
+ foreach ($obj->getMembers() as $memb)
+ {
+ if ($memb->guid == $this->guid)
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent is assigned to the given issue
+ */
+ public function isAssignedTo(issue $issue) : bool
+ {
+ foreach ($issue->getAssignees() as $assign)
+ {
+ if ($assign->assignee->guid == $this->guid
+ && $assign->dismissed == "")
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Send an email message to this agent using stored configuration
+ * parameters. If config is not established, delivery is not
+ * attempted. Return status.
+ */
+ public abstract function sendEmail(string $subj, string $mesg,
+ ?string $attachPath = NULL, ?string $attachName = NULL,
+ bool $ignoreEmailConf = false) : bool;
+
+ /*
+ * Get the display name for this agent. For groups this is the
+ * object name; for users, this is the object name, unless an
+ * alias is set.
+ */
+ public function getDisplayName() : string
+ {
+ if ($this->objtype != "user")
+ return $this->name;
+
+ $user = new user($this->guid);
+
+ if ($user->alias != "")
+ return $user->alias;
+
+ return $user->name;
+ }
+
+ /*
+ * Get all pads this agent owns or is a member of. This isn't
+ * necessarily all pads this agent has access permission for.
+ * Results are sorted by ownership, then by name.
+ */
+ public function getPads_ordByOwnByName() : array
+ {
+ $pads = array();
+
+ /* owner */
+ $query = "SELECT guid FROM objects WHERE objtype = 'pad' AND " .
+ "owner = '" . database::esc($this->guid) . "' ORDER BY name";
+ $res = database::query($query);
+
+ foreach ($res as $p)
+ $pads[] = new pad($p['guid']);
+
+ /* members */
+ $query = "SELECT o.guid FROM objects o JOIN members m ON " .
+ "o.guid = m.guid WHERE o.objtype = 'pad' AND " .
+ "m.member = '" . database::esc($this->guid) . "' ORDER BY o.name";
+ $res = database::query($query);
+
+ foreach ($res as $p)
+ $pads[] = new pad($p['guid']);
+
+ return $pads;
+ }
+
+ /*
+ * Check whether this agent has access permission for given
+ * object
+ */
+ public function canAccess(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($this->isMemberOf($obj))
+ return true;
+
+ if ($obj->pubAcc)
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canAccessSub($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canAccessSub($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has modify permission for given
+ * object
+ */
+ public function canModify(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($this->isMemberOf($obj) && $obj->membModify)
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canModifySub($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canModifySub($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has modify members permission for
+ * given object
+ */
+ public function canModifyMembers(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($this->isMemberOf($obj) && $obj->membMemb)
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canModifySubMembers($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canModifySubMembers($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has modify permissions permission
+ * for given object
+ */
+ public function canModifyPermissions(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canModifySubPermissions($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canModifySubPermissions($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has access-sub permission for
+ * given object
+ */
+ public function canAccessSub(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($this->isMemberOf($obj) && $obj->membAccs)
+ return true;
+
+ if ($obj->pubAccs)
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canAccessSub($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canAccessSub($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has create-sub permission
+ * for given object
+ */
+ public function canCreateSub(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($this->isMemberOf($obj) && $obj->membCres)
+ return true;
+
+ if ($obj->pubCres)
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canCreateSub($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canCreateSub($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has modify-sub permission
+ * for given object
+ */
+ public function canModifySub(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($this->isMemberOf($obj) && $obj->membModifys)
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canModifySub($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canModifySub($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has modify-sub-members
+ * permission for given object
+ */
+ public function canModifySubMembers(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($this->isMemberOf($obj) && $obj->membMembs)
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canModifySubMembers($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canModifySubMembers($owner))
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * Check whether this agent has modify-sub-permissions
+ * permission for given object
+ */
+ public function canModifySubPermissions(obj $obj) : bool
+ {
+ if ($this->admin)
+ return true;
+
+ if ($this->guid == $obj->guid)
+ return true;
+
+ if ($this->isOwnerOf($obj))
+ return true;
+
+ if ($obj->parent)
+ {
+ $parent = new obj($obj->parent);
+ if ($this->canModifySubPermissions($parent))
+ return true;
+ }
+ else if ($obj->owner)
+ {
+ $owner = new obj($obj->owner);
+ if ($this->canModifySubPermissions($owner))
+ return true;
+ }
+
+ return false;
+ }
+}
+
+?>