Ejemplo n.º 1
0
 /**
 * Finds and gets full userinfo by filtering inside the given container
 * Note that this method is not particularily efficient, as it fetches
 * the data in the primary container in a single call, but requires one call
 * to the secondary container for every user returned from the primary container
 *
 * @param  array params (as for getUsers()
 *          with an additional optional key 'container' 'perm' (default) or
            'auth' to determine the primary and secondary container.
            data is first fetched from the primary container and then
            combined with data from the secondary container if available
 * @return array|bool array with userinfo if found on success or false otherwise
 *
 * @access public
 */
 function getUsers($params = array())
 {
     $params = LiveUser_Admin_Storage::setSelectDefaultParams($params);
     if ($params['select'] != 'row' && $params['select'] != 'all') {
         $this->stack->push(LIVEUSER_ADMIN_ERROR, 'exception', array('msg' => 'Select must be "row" or "all"'));
         return false;
     }
     if (array_key_exists('container', $params) && $params['container'] == 'auth') {
         return $this->_getUsersByAuth($params);
     }
     return $this->_getUsersByPerm($params);
 }
Ejemplo n.º 2
0
 /**
  * This function holds up most of the heat for all the get* functions.
  *
  * @param array containing key-value pairs for:
  *                 'fields'  - ordered array containing the fields to fetch
  *                             if empty all fields from the user table are fetched
  *                 'filters' - key values pairs (value may be a string or an array)
  *                 'orders'  - key value pairs (values 'ASC' or 'DESC')
  *                 'rekey'   - if set to true, returned array will have the
  *                             first column as its first dimension
  *                 'group'   - if set to true and $rekey is set to true, then
  *                             all values with the same first column will be
  *                             wrapped in an array
  *                 'limit'   - number of rows to select
  *                 'offset'  - first row to select
  *                 'select'  - determines what query method to use:
  *                             'one' -> queryOne, 'row' -> queryRow,
  *                             'col' -> queryCol, 'all' ->queryAll (default)
  * @param string name of the table from which to start looking
  *               for join points
  * @param array list of tables that may be joined to
  * @return bool|array false on failure or array with selected data
  *
  * @access private
  */
 function _makeGet($params, $root_table, $selectable_tables)
 {
     // Ensure that default params are set
     $params = LiveUser_Admin_Storage::setSelectDefaultParams($params);
     $data = $this->_storage->select($params['select'], $params['fields'], $params['filters'], $params['orders'], $params['rekey'], $params['group'], $params['limit'], $params['offset'], $root_table, $selectable_tables);
     // If 'with' is set and the result data is not empty
     if (!empty($params['with']) && !empty($data)) {
         if ($params['select'] != 'all') {
             $this->stack->push(LIVEUSER_ADMIN_ERROR, 'exception', array('msg' => 'Using "with" requires "select" to be set to "all"'));
             return false;
         }
         // Check if all with keys were fetched
         $missing = array_diff(array_keys($params['with']), array_keys(reset($data)));
         if (!empty($missing)) {
             $this->stack->push(LIVEUSER_ADMIN_ERROR, 'exception', array('msg' => 'The following "with" elements are not included in the result: ' . implode(', ', $missing)));
             return false;
         }
         foreach ($data as $key => $row) {
             foreach ($params['with'] as $field => $with_params) {
                 $with_params['filters'][$field] = $row[$field];
                 $method = $this->withFieldMethodMap[$field];
                 // remove "_id" from the field name (group_id => group)
                 $data_key = preg_replace('/(.+)_id/', '\\1s', $field);
                 $data[$key][$data_key] = $this->{$method}($with_params);
             }
         }
     }
     return $data;
 }
Ejemplo n.º 3
0
 /**
  * properly disconnect from resources
  *
  * @access  public
  */
 function disconnect()
 {
     $this->_storage->disconnect();
 }
Ejemplo n.º 4
0
 /**
 * Fetches rights
 *
 * @param array containing key-value pairs for:
 *                 'fields'  - ordered array containing the fields to fetch
 *                             if empty all fields from the user table are fetched
 *                 'filters' - key values pairs (value may be a string or an array)
 *                 'orders'  - key value pairs (values 'ASC' or 'DESC')
 *                 'rekey'   - if set to true, returned array will have the
 *                             first column as its first dimension
 *                 'group'   - if set to true and $rekey is set to true, then
 *                             all values with the same first column will be
 *                             wrapped in an array
 *                 'limit'   - number of rows to select
 *                 'offset'  - first row to select
 *                 'select'  - determines what query method to use:
 *                             'one' -> queryOne, 'row' -> queryRow,
 *                             'col' -> queryCol, 'all' ->queryAll (default)
 *                 'selectable_tables' - array list of tables that may be
 *                             joined to in this query, the first element is
 *                             the root table from which the joins are done
 *                 'by_group' - if joins should be done using the 'userrights'
 *                             (false default) or through the 'grouprights'
 *                             and 'groupusers' tables (true)
 *                 'inherited' - filter array to fetch all rughts from
                                (sub)group membership
 *                 'implied'  - filter array for fetching implied rights
 *                 'hierarchy' - filter array for fetching implied rights
                               into a nested array (overwrites 'implied')
 * @return bool|array false on failure or array with selected data
 *
 * @access public
 */
 function getRights($params = array())
 {
     // Determine of 'inherited', 'implied' or 'hierarchy' is set
     // 'hierarchy' means that 'implied' also is set
     $inherited = array_key_exists('inherited', $params);
     if (array_key_exists('hierarchy', $params)) {
         $hierarchy = $implied = true;
         $params['implied'] = $params['hierarchy'];
     } else {
         $implied = array_key_exists('implied', $params);
         $hierarchy = false;
     }
     // Sanity check on the provided params if the inherited of implied param is set.
     if ($inherited || $implied) {
         $params = LiveUser_Admin_Storage::setSelectDefaultParams($params);
         if (!$params['rekey'] || $params['group'] || $params['select'] !== 'all' || reset($params['fields']) !== 'right_id' && reset($params['fields']) !== '*') {
             $this->stack->push(LIVEUSER_ADMIN_ERROR, 'exception', array('msg' => "Setting 'inherited', 'implied' or 'hierarchy'" . " is only allowed if 'rekey' is enabled, 'group' is disabled" . ", 'select' is 'all' and the first field is 'right_id'"));
             return false;
         }
         // Extra sanity check on the provided params if the implied param is set
         // (has_implied should be included in the fields.
         if ($implied && !in_array('has_implied', $params['fields'])) {
             $this->stack->push(LIVEUSER_ADMIN_ERROR, 'exception', array('msg' => "Setting 'implied' or 'hierarchy' requires that 'has_implied' field needs to be in the select list"));
             return false;
         }
     }
     // handle select, fields and rekey
     $rights = parent::getRights($params);
     if ($rights === false) {
         return false;
     }
     // read rights inherited by (sub)groups
     if ($inherited) {
         // todo: consider adding a NOT IN filter
         $inherited_rights = $this->_getInheritedRights($params);
         if ($inherited_rights === false) {
             return false;
         }
         if (!empty($inherited_rights)) {
             // Loop through the resulting inherited rights, check if they already exist in the current rights array
             // If not: set the type to inherited and add it to the rights array.
             foreach ($inherited_rights as $right_id => $right) {
                 if (isset($rights[$right_id])) {
                     continue;
                 }
                 $right['_type'] = 'inherited';
                 $rights[$right_id] = $right;
             }
         }
     }
     // if the result was empty or no additional work is needed
     if (empty($rights) || !$implied) {
         return $rights;
     }
     if ($implied) {
         $_rights = $rights;
         $rights = array();
         // Loop through the current rights array (backupped in _rights) and
         // set the type to granted if the type isn't set yet.
         foreach ($_rights as $right_id => $right) {
             if (!array_key_exists('_type', $right)) {
                 $right['_type'] = 'granted';
             }
             $rights[$right_id] = $right;
             // If has_implied isn't true, continue (no work to do).
             if (!$right['has_implied']) {
                 continue;
             }
             // todo: consider adding a NOT IN filter
             $implied_rights = $this->_getImpliedRights($params, $right_id);
             if ($implied_rights === false) {
                 return false;
             } elseif (empty($implied_rights)) {
                 continue;
             }
             // Loop through the resulting rights, set the type and order the array based
             // on the params['implied'] value.
             foreach ($implied_rights as $implied_right_id => $right) {
                 if (isset($rights[$implied_right_id])) {
                     continue;
                 }
                 $right['_type'] = 'implied';
                 // If hierarchy: add the resulting rights to the right they belong to (in implied_rights)
                 if ($hierarchy) {
                     $rights[$right_id]['implied_rights'][$implied_right_id] = $right;
                 } else {
                     $rights[$implied_right_id] = $right;
                 }
             }
         }
         return $rights;
     }
     $params = LiveUser_Admin_Storage::setSelectDefaultParams($params);
     // If the select is set to all (or not set at all) and more than one field is set,
     // set the type to granted if it isn't set.
     if ($params['select'] == 'all' && (count($params['fields']) > 1 || reset($params['fields']) === '*')) {
         foreach ($rights as $right_id => $right) {
             if (!isset($rights[$right_id]['_type']) || !$rights[$right_id]['_type']) {
                 $rights[$right_id]['_type'] = 'granted';
             }
         }
     }
     return $rights;
 }