"Fossies" - the Fresh Open Source Software Archive

Member "Open-Web-Analytics-1.7.0/modules/base/classes/serviceUser.php" (16 Sep 2020, 9185 Bytes) of package /linux/www/Open-Web-Analytics-1.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "serviceUser.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.6.9_vs_1.7.0.

    1 <?php 
    2 
    3 //
    4 // Open Web Analytics - An Open Source Web Analytics Framework
    5 //
    6 // Copyright 2008 Peter Adams. All rights reserved.
    7 //
    8 // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
    9 //
   10 // Unless required by applicable law or agreed to in writing, software
   11 // distributed under the License is distributed on an "AS IS" BASIS,
   12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13 // See the License for the specific language governing permissions and
   14 // limitations under the License.
   15 //
   16 // $Id$
   17 //
   18 
   19 /**
   20  * Service User Class
   21  * 
   22  * @author      Peter Adams <peter@openwebanalytics.com>
   23  * @copyright   Copyright &copy; 2008 Peter Adams <peter@openwebanalytics.com>
   24  * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
   25  * @category    owa
   26  * @package     owa
   27  * @version        $Revision$
   28  * @since        owa 1.0.0
   29  */
   30 
   31 
   32 class owa_serviceUser extends owa_base {
   33     /**
   34      * @var owa_user
   35      */
   36     public $user;
   37     var $capabilities = array();
   38     var $preferences = array();
   39     var $is_authenticated = false;
   40     public $assignedSites = array();
   41     private $isInitialized = false;
   42     private $isAssignedSitesListLoaded = false;
   43 
   44     function __construct() {
   45         //parent::__construct();
   46         // create empty user entity
   47         $this->user = owa_coreApi::entityFactory('base.user');
   48         // set default role
   49         $this->setRole('everyone');
   50     }
   51 
   52     /**
   53      * Loads Current user based on user_id
   54      * This method should only used if the user is authenticated.
   55      *
   56      * @param $user_id    string    the user_id
   57      * @depricated
   58      */
   59     function load( $user_id = '' ) {
   60 
   61         if (! $user_id ) {
   62 
   63             // if there is no user_id and role is everyone
   64             // procead with loading sites and
   65             //if ( $this->isAnonymousUser() ) {
   66             //    return $this->initInternalProperties();
   67             //} else {
   68                 throw new Exception('No valid userid given!');
   69             //}
   70         }
   71 
   72         // if there is a user_id load the user object and other properties.
   73         $this->user->load($user_id, 'user_id');
   74         $this->initInternalProperties();
   75     }
   76 
   77     /**
   78      * Loads the current user from an owa_user object
   79      * owa_auth uses this after the user is authenticated
   80      *
   81      * @param $user_obj    object    owa_user object
   82      */
   83     function loadNewUserByObject($user_obj) {
   84         $this->user = $user_obj;
   85         $this->initInternalProperties();
   86     }
   87 
   88     private function initInternalProperties() {
   89         $this->loadRelatedUserData();
   90         $this->loadAssignedSites();
   91         $this->setInitialized();
   92     }
   93 
   94     function loadRelatedUserData() {
   95         $this->capabilities = $this->getCapabilities($this->user->get('role'));
   96         $this->preferences = $this->getPreferences($this->user->get('user_id'));
   97 
   98     }
   99     /**
  100      * gets allowed capabilities for the user role
  101      * @param unknown_type $role
  102      */
  103     function getCapabilities($role) {
  104         return owa_coreAPI::getCapabilities( $role );
  105     }
  106 
  107     function getPreferences($user_id) {
  108         return false;
  109     }
  110 
  111     function getRole() {
  112         return $this->user->get('role');
  113     }
  114 
  115     /**
  116      * Sets role and related capabilities
  117      *
  118      * @param    $value    string    the user's role
  119      */
  120     function setRole($value) {
  121 
  122         $this->user->set('role', $value);
  123         $this->capabilities = $this->getCapabilities($value);
  124     }
  125 
  126     function setUserData($name, $value) {
  127 
  128         $this->user->set($name, $value);
  129     }
  130 
  131     function getUserData($name) {
  132 
  133         return $this->user->get($name);
  134     }
  135 
  136     /**
  137      * Checks if user has a partciular capability
  138      *
  139      * @param string     $cap
  140      * @param integer     $siteId    only needed if capability requires site access. you need to pass site_id (not id) field
  141      * @return boolean
  142      */
  143     function isCapable($cap, $siteId = null) {
  144         owa_coreAPI::debug("Checking if user is capable of: ".$cap);
  145 
  146         // is this capability assigned to everyone?
  147         // is this the global admin user?
  148         // was no capability passed?
  149         // if so, the user can see and do everything
  150         if ( owa_coreAPI::isEveryoneCapable( $cap ) || $this->user->isAdmin() || empty($cap)) {
  151             owa_coreAPI::debug('No capability passed or user is an admin and capable of everything.');
  152             return true;
  153         }
  154 
  155         // is this user's role capable?
  156         if (!in_array($cap, $this->capabilities)) {
  157             owa_coreAPI::debug('capability does not exist for this role. user is not capable');
  158             return false;
  159         }
  160 
  161         // Does capability also require site access?
  162         if ( $this->isSiteAccessRequiredForCapability( $cap ) ) {
  163             owa_coreAPI::debug('Site access required for this capability.');
  164             if ( ! $this->isSiteAccessible( $siteId ) ) {
  165                 owa_coreAPI::debug('Site is not accessible for this user.');
  166                 return false;
  167             } else {
  168                 owa_coreAPI::debug('Site is accessible for this user.');
  169             }
  170         }
  171 
  172         return true;
  173     }
  174 
  175     /**
  176      * Checks to see if the Capability requires
  177      * user to pass site access control check
  178      *
  179      * @param    $capability    string    the name of the capability (e.g. 'view_reports')
  180      * @return    boolean
  181      */
  182     function isSiteAccessRequiredForCapability( $capability ) {
  183 
  184         $capabilitiesThatRequireSiteAccess = owa_coreAPI::getSetting('base', 'capabilitiesThatRequireSiteAccess');
  185         if (is_array($capabilitiesThatRequireSiteAccess) && in_array($capability, $capabilitiesThatRequireSiteAccess)) {
  186             return true;
  187         }
  188     }
  189 
  190     /**
  191      * Checks to see if the a site is accessible to a user
  192      *
  193      * @param    string    $siteId    the siteId of the site in question
  194      * @return    boolean
  195      */
  196     function isSiteAccessible( $siteId ) {
  197 
  198         if ( is_null($siteId) ) {
  199             throw new InvalidArgumentException('Cannot tell if site is accessible to user without a siteId (none given).');
  200         }
  201 
  202         if ( $this->user->isAdmin() ) {
  203             return true;
  204         }
  205 
  206         if ( ! $this->isAssignedSitesListLoaded ) {
  207             //$this->loadAssignedSites();
  208         }
  209 
  210         if ( isset( $this->assignedSites[ $siteId ] ) ) {
  211             owa_coreAPI::debug("Site ID: $siteId in accessible list for this user.");
  212             return true;
  213         } else {
  214             owa_coreAPI::debug("Site ID: $siteId is not in accessible list for this user.");
  215         }
  216     }
  217 
  218     // mark the user as authenticated and populate their capabilities
  219     function setAuthStatus($bool) {
  220         $this->is_authenticated = true;
  221     }
  222 
  223     function isAuthenticated() {
  224         return $this->is_authenticated;
  225     }
  226 
  227 
  228     /**
  229      * Loads internal $this->assignedSites member
  230      */
  231     private function loadAssignedSites() {
  232         owa_coreAPI::debug('loading assigned sites');
  233         
  234         try {
  235             
  236             if ( ! $this->user->get( 'id' ) ) {
  237                  throw new Exception('no user object loaded!');
  238             }    
  239             
  240         }
  241         
  242         catch( Exception $e ) {
  243             
  244             owa_coreAPI::debug('Handled exception: '. $e->getMessage() );
  245             
  246         }
  247 
  248         $site_ids = array();
  249         $db = owa_coreAPI::dbSingleton();
  250         $db->selectFrom( 'owa_site_user' );
  251         $db->selectColumn( '*' );
  252         $db->where( 'user_id', $this->user->get('id') );
  253         $site_ids = $db->getAllRows();
  254 
  255         // filter array of site_ids.
  256         $dispatch = owa_coreAPI::getEventDispatch();
  257         $site_ids = $dispatch->filter('allowed_sites_list', $site_ids);
  258 
  259         $this->setAllowedSitesList($site_ids);
  260     }
  261 
  262     public function setInitialized() {
  263         $this->isInitialized = true;
  264     }
  265 
  266     public function loadAssignedSitesByDomain($domains) {
  267 
  268         if ( $domains ) {
  269             $site_ids = array();
  270             $s = owa_coreApi::entityFactory('base.site');
  271 
  272             foreach ($domains as $domain) {
  273 
  274                 $site_ids[] = array('site_id' => $s->generateId( $s->generateSiteId( $domain ) ) );
  275             }
  276 
  277             $this->setAllowedSitesList($site_ids);
  278         }
  279     }
  280 
  281     private function setAllowedSitesList($site_ids) {
  282 
  283         $list = array();
  284 
  285         if ( ! empty($site_ids) ) {
  286             foreach ($site_ids as $row) {
  287                 $siteEntity = owa_coreApi::entityFactory('base.site');
  288                 $siteEntity->load($row['site_id']);
  289                 $list[ $siteEntity->get('site_id') ] = $siteEntity;
  290             }
  291         }
  292 
  293         $this->assignedSites = $list;
  294         $this->isAssignedSitesListLoaded = true;
  295     }
  296 
  297     public function getAssignedSites() {
  298 
  299         return $this->assignedSites;
  300     }
  301 
  302 
  303     public function isOWAAdmin() {
  304 
  305         return $this->user->isOWAAdmin();
  306     }
  307 
  308     public function isAdmin() {
  309 
  310         return $this->user->isAdmin();
  311     }
  312 
  313     public function isAnonymousUser() {
  314 
  315         if ( ! $this->user->get('user_id') || $this->getRole() === 'everyone') {
  316             return true;
  317         } else {
  318             return false;
  319         }
  320     }
  321 }
  322 
  323 
  324 
  325 ?>