Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acl xduser #146

Merged
merged 1 commit into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions classes/Models/Acl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php namespace Models;

use CCR\DB;

/**
* Class Acl
*
* Represents a named grouping under which a selection of 'Assets' can be
* secured and to which a number of users can be belong. Data for this class
* is stored in the 'acls' table while the relationship of user to acl is stored
* in 'user_acls'.
*
* @package User
*
* The 'getters' and 'setters' for this class:
* @method integer getAclId()
* @method void setAclId($aclId)
* @method integer getModuleId()
* @method void setModuleId($moduleId)
* @method integer getAclTypeId()
* @method void setAclTypeId($aclTypeId)
* @method string getName()
* @method void setName($name)
* @method string getDisplay()
* @method void setDisplay($display)
* @method boolean getEnabled()
* @method void setEnabled($enabled)
* @method integer getUserId()
* @method void setUserId($userId)
*
*/
class Acl extends DBObject
{
protected $PROP_MAP = array(
'acl_id' => 'aclId',
'module_id' => 'moduleId',
'acl_type_id' => 'aclTypeId',
'name' => 'name',
'display' => 'display',
'enabled' => 'enabled',

// Needed for getParameters
'user_id' => 'userId'
);

/**
* @return array|null
* @throws \Exception
*/
public function getParameters()
{
$userId = $this->getUserId();
if (!isset($userId)) {
throw new \Exception('Acl has no user_id. Cannot retrieve parameters');
}
$aclId = $this->getAclId();
if (!isset($aclId)) {
throw new \Exception('Acl has no acl_id. Cannot retrieve parameters');
}

$db = DB::factory('database');

$query =<<< SQL
SELECT DISTINCT
uagbp.user_id,
uagbp.acl_id,
gb.name,
'=',
uagbp.value
FROM user_acl_group_by_parameters uagbp
JOIN user_acls ua
ON uagbp.user_id = ua.user_id
AND uagbp.acl_id = ua.acl_id
JOIN group_bys gb
ON gb.group_by_id = uagbp.group_by_id
WHERE
ua.user_id = :user_id
AND ua.acl_id = :acl_id
SQL;
$rows = $db->query($query, array(':user_id' => $userId, ':acl_id' => $aclId));
if (false !== $rows) {
$results = array_reduce($rows, function ($carry, $item) {
$carry[$item['name']] = $item['value'];
return $carry;
}, $rows);
return $results;
}

return null;
}

/**
* Determine if the user who has access to this acl
*
* @param $query_groupname
* @param null $realm_name
* @param null $group_by_name
* @param null $statistic_name
* @return bool
* @throws \Exception
*/
public function hasDataAccess($query_groupname, $realm_name = null, $group_by_name = null, $statistic_name = null)
{
$userId = $this->getUserId();
if (null === $userId) {
throw new \Exception('Acl has no user_id. Cannot check data access');
}

$params = array(
':user_id' => $userId
);

$hasRealm = isset($realm_name);
$hasGroupBy = isset($group_by_name);
$hasStatistic = isset($statistic_name);

$query =<<<SQL
SELECT
agb.*,
r.realm_id,
r.name AS realm,
gb.group_by_id,
gb.name AS group_by
FROM acl_group_bys agb
JOIN user_acls ua
ON agb.acl_id = ua.acl_id
LEFT JOIN realms r
ON agb.realm_id = r.realm_id
LEFT JOIN group_bys gb
ON agb.group_by_id = gb.group_by_id
LEFT JOIN statistics s
ON agb.statistic_id = s.statistic_id
WHERE
ua.user_id = :user_id
AND agb.visible = TRUE
AND agb.enabled = TRUE
SQL;
if (true === $hasRealm) {
$query.= " AND r.name = LOWER(:realm_name)\n";
$params[':realm_name'] = $realm_name;
}
if (true === $hasGroupBy) {
$query .= " AND gb.name = :group_by_name\n";
$params[':group_by_name'] = $group_by_name;
}
if (true === $hasStatistic) {
$query .= " AND s.name = :statistic_name\n";
$params[':statistic_hame'] = $statistic_name;
}

$db = DB::factory('database');
$rows = $db->query($query, $params);

return $rows !== false && count($rows) > 0;
}
}
73 changes: 73 additions & 0 deletions classes/Models/DBObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php namespace Models;

/**
* Class DBObject
*
* The intent of this class is to provide an easy way for child classes, which
* are meant to represent the data contained within one row of a table,
* an easy way of interacting with a PDO result set in which the rows have been
* returned as arrays. In particular, this allows the knowledge of what is
* expected / contained in these tables / classes to be defined at particular
* point in time (i.e. git commit ) as opposed to spread throughout the code
* utilizing these objects. It also allows the utilizing code to interact with
* the class and its associated properties / functions as opposed to a simple
* array.
*
* On a more technical note, it provides dynamic 'getter' and 'setter'
* support for calls that follow the form 'getCamelCasePropertyName()' and
* 'setCamelCasePropertyName($propertyName)' the property name is assumed to be
* in the form: lcfirst(CamelCase(column_name)) => columnName.
*
* And for those who enjoy working with their classes in an array type manner.
* ArrayAccess has been implemented such that 'offsetGet' corresponds to
* 'getCamelCasePropertyName()', 'offsetSet' corresponds to
* 'setCamelCasePropertyName($propertyName)' and 'offsetExists($offset)'
* ensures that the '$offset' is defined in the $PROP_MAP and that there
* is a property currently defined with a name that that matches '$offset';
*
* @author Ryan Rathsam <ryanrath@buffalo.edu>
*/
class DBObject
{

protected $PROP_MAP = array();

/**
* Default Constructor
*
* @param array $options the options used to configure this instance.
**/
public function __construct($options = array())
{
$properties = $this->PROP_MAP;

foreach ($properties as $property => $value) {
if (array_key_exists($property, $options)) {
$this->$value = $options[$property];
}
}
}

/**
* @inheritDoc
**/
public function __call($name, $arguments)
{
/* The following block of code dynamically generates 'getters' and
* 'setters' based on the properties the class currently supports.
* This frees child classes from needing to clutter their class space
* with boiler plate functions.
*/

$var = lcfirst(substr($name, 3));
if ((strncasecmp($name, 'get', 3) === 0) &&
property_exists($this, $var)
) {
return $this->$var;
} elseif ((strncasecmp($name, 'set', 3) === 0) &&
(property_exists($this, $var))
) {
$this->$var = $arguments[0];
}
}
}
57 changes: 57 additions & 0 deletions classes/Models/GroupBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace Models;

/**
* Class GroupBy
*
* @method integer getGroupById()
* @method void setGroupById($groupById)
* @method integer getModuleId()
* @method void setModuleId($moduleId)
* @method integer getRealmId()
* @method void setRealmId($realmId)
* @method string getName()
* @method void setName($name)
* @method string getDisplay()
* @method void setDisplay($display)
* @method string getDescription()
* @method void setDescription($description)
* @method string getSchemaName()
* @method void setSchemaName($schemaName)
* @method string getTableName()
* @method void setTableName($tableName)
* @method string getAlias()
* @method void setAlias($alias)
* @method string getIdColumn()
* @method void setIdColumn($idColumn)
* @method string getNameColumn()
* @method void setNameColumn($nameColumn)
* @method string getShortnameColumn()
* @method void setShortnameColumn($shortnameColumn)
* @method string getOrderIdColumn()
* @method void setOrderIdColumn($orderIdColumn)
* @method string getFkColumn()
* @method void setFkColumn($fkColumn)
* @method string getClazz()
* @method void setClazz($clazz)
*/
class GroupBy extends DBObject
{

protected $PROP_MAP = array(
'group_by_id'=> 'groupById',
'module_id' => 'moduleId',
'realm_id' => 'realmId',
'name' => 'name',
'display' => 'display',
'description' => 'description',
'schema_name'=> 'schemaName',
'table_name' => 'tableName',
'alias'=> 'alias',
'id_column' => 'idColumn',
'name_column' => 'nameColumn',
'shortname_column' => 'shortnameColumn',
'order_id_column' => 'orderIdColumn',
'fk_column' => 'fkColumn',
'class' => 'clazz'
);
}
30 changes: 30 additions & 0 deletions classes/Models/Realm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php namespace Models;

/**
* Class Realm
*
* the 'getters' and 'setters' for this class:
* @method integer getRealmId()
* @method void setRealmId($realmId)
* @method integer getModuleId()
* @method void setModuleId($moduleId)
* @method string getName()
* @method void setName($name)
* @method string getDisplay()
* @method void setDisplay($display)
* @method string getTableName()
* @method void setTableName($tableName)
* @method string getSchemaName()
* @method void setSchemaName($schemaName)
*/
class Realm extends DBObject
{
protected $PROP_MAP = array(
'realm_id'=> 'realmId',
'module_id' => 'moduleId',
'name' => 'name',
'display'=> 'display',
'schema_name' => 'schemaName',
'table_name' => 'tableName'
);
}
Loading