BEdita API doc | 4-cactus
  • Class
  • Tree
  • Deprecated
  • Download

Classes

  • Addon
  • AddressbookController
  • AddressbookShell
  • AdminController
  • Alias
  • Annotation
  • ApiAuthComponent
  • ApiBaseController
  • ApiFormatterComponent
  • ApiUploadComponent
  • ApiValidatorComponent
  • AppController
  • AppError
  • AppHelper
  • Application
  • AppModel
  • Area
  • AreasController
  • Audio
  • AuthenticationsController
  • BannedIp
  • BEAppModel
  • BEAppObjectModel
  • BeAuthComponent
  • BeAuthFacebookComponent
  • BeAuthGoogleComponent
  • BeAuthTwitterComponent
  • BeCallbackManager
  • BeConfigure
  • BeCustomPropertyComponent
  • BeditaAnnotationModel
  • BeditaBaseShell
  • BeditaCollectionModel
  • BeditaContentModel
  • BeditaExportFilter
  • BeditaImportFilter
  • BeditaObjectModel
  • BeditaProductModel
  • BeditaShell
  • BeditaSimpleObjectModel
  • BeditaSimpleStreamModel
  • BeditaStreamModel
  • BeEmbedFlashHelper
  • BeEmbedHtml5Helper
  • BeEmbedMediaHelper
  • BeErrorCode
  • BeExceptionHandler
  • BEFile
  • BeFileHandlerComponent
  • BeFormHelper
  • BeFrontHelper
  • BeHashComponent
  • BeHtmlHelper
  • BeLangTextComponent
  • BeLib
  • BeMailComponent
  • BEObject
  • BeObjectCache
  • BePaginatorToolbarHelper
  • BeSchema
  • BeSecurityComponent
  • BeSystem
  • BeSystemComponent
  • BeThumb
  • BeTimeHelper
  • BeToolbarHelper
  • BeTreeComponent
  • BeTreeHelper
  • BeUploadToObjComponent
  • BeurlHelper
  • BeVimeoComponent
  • BeYoutubeComponent
  • BuildFilterBehavior
  • CacheableBehavior
  • CacheShell
  • CallbackBehavior
  • CaptchaComponent
  • Caption
  • CaptionsBehavior
  • Card
  • CardsImportFilter
  • Category
  • CleanupDataTask
  • CleanupTask
  • Comment
  • CommentsController
  • CompactResultBehavior
  • Content
  • CreateIndexFieldsBehavior
  • DataShell
  • DataTransfer
  • DateItem
  • DbadminShell
  • DeleteDependentObjectBehavior
  • DeleteObjectBehavior
  • DeployShell
  • Document
  • DocumentsController
  • DumpModel
  • EditorialContentsShell
  • EditorNote
  • Event
  • EventLog
  • EventsController
  • FilesController
  • ForeignDependenceSaveBehavior
  • FrontendController
  • FrontendShell
  • GalleriesController
  • Gallery
  • GeoTag
  • GettextShell
  • Git
  • GravatarHelper
  • Group
  • HashJob
  • History
  • HomeController
  • Image
  • ImageInfoHelper
  • JsonExportFilter
  • JsonImportFilter
  • JsonView
  • LangText
  • Link
  • MailGroup
  • MailGroupCard
  • MailJob
  • MailLog
  • MailMessage
  • MailShell
  • MailTemplate
  • Mask
  • MediaShell
  • MigrateDumpModel
  • MigrateShell
  • Migration
  • MigrationBase
  • MigrationDateItemsShell
  • Module
  • ModulesController
  • ModuleShell
  • MultimediaController
  • NewsController
  • NewsletterController
  • NewsletterShell
  • NotifyBehavior
  • ObjectEditor
  • ObjectProperty
  • ObjectRelation
  • ObjectType
  • ObjectUser
  • PagesController
  • Permission
  • PermissionModule
  • PermsHelper
  • Product
  • Property
  • PropertyOption
  • PublicationShell
  • RedisShell
  • RelationCheck
  • RelationRepair
  • RelationShell
  • RelationStats
  • RemoveDummyNameBehavior
  • ResponseHandlerComponent
  • RestClientModel
  • Revision
  • RevisionObjectBehavior
  • SearchText
  • SearchTextSaveBehavior
  • Section
  • SectionDummy
  • SessionFilterComponent
  • SessionFilterHelper
  • ShortNews
  • SmartyView
  • SoapClientModel
  • SplitterSql
  • StatisticsController
  • Stream
  • StreamLog
  • StreamsController
  • Svn
  • TagsController
  • ThemeSmartyView
  • ThumbnailHelper
  • ThumbShell
  • TransactionComponent
  • TransactionFS
  • TranslationsController
  • Tree
  • TrHelper
  • User
  • UserProperty
  • UsersController
  • Utility
  • Version
  • Video
  • VimeoHelper
  • WebmarksController
  • XmlExportFilter
  • XmlImportFilter
  • XmlJsonConverter
  • XmlView
  • YoutubeHelper

Interfaces

  • ApiAuthInterface
  • MediaProviderInterface
  • UploadableInterface

Exceptions

  • BeditaAjaxException
  • BEditaAllowURLException
  • BeditaBadRequestException
  • BeditaConflictException
  • BEditaDeleteStreamObjException
  • BeditaException
  • BEditaFileExistException
  • BeditaForbiddenException
  • BeditaFrontAccessException
  • BeditaHashException
  • BEditaInfoException
  • BeditaInternalErrorException
  • BEditaIOException
  • BeditaLengthRequiredException
  • BeditaMailException
  • BEditaMediaProviderException
  • BeditaMethodNotAllowedException
  • BEditaMIMEException
  • BeditaNotFoundException
  • BeditaNotImplementedException
  • BeditaPublicationException
  • BeditaRuntimeException
  • BEditaSaveStreamObjException
  • BeditaServiceUnavailableException
  • BeditaUnauthorizedException
  • BEditaUploadPHPException
  • BEditaURLException
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 
<?php
/*-----8<--------------------------------------------------------------------
 * 
 * BEdita - a semantic content management framework
 * 
 * Copyright 2008 ChannelWeb Srl, Chialab Srl
 * 
 * This file is part of BEdita: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * BEdita is distributed WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License 
 * version 3 along with BEdita (see LICENSE.LGPL).
 * If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
 * 
 *------------------------------------------------------------------->8-----
 */

/**
 * 
 *
 * @version         $Revision$
 * @modifiedby      $LastChangedBy$
 * @lastmodified    $LastChangedDate$
 * 
 * $Id$
 */
class Permission extends BEAppModel
{
    
    var $belongsTo = array(
        'User' =>
            array(
                'className'     => 'User',
                'conditions'    => "Permission.switch = 'user' ",
                'foreignKey'    => 'ugid'
            ),
        'Group' =>
            array(
                'className'     => 'Group',
                'conditions'    => "Permission.switch = 'group' ",
                'foreignKey'    => 'ugid'
            ),
    );

    /**
     * Object cache 
     * 
     */
    protected $BeObjectCache = null;

    public function  __construct() {
        parent::__construct();
        if (!BACKEND_APP && Configure::read('objectCakeCache') && !Configure::read('staging')) {
            App::import('Lib', 'BeLib');
            $this->BeObjectCache = BeLib::getObject('BeObjectCache');
        }
    }

    /**
     * Add object permissions
     *
     * @param integer $objId    object id
     * @param array $perms      array like (array("flag"=>1, "switch" => "group", "name" => "guest"), array(...))
     */
    public function add($objectId, $perms) {
        foreach ($perms as $d) {
            $d["object_id"] = $objectId;
            $this->create();
            if($d["switch"] == "group") {
                $group = ClassRegistry::init("Group");
                $d["ugid"] = $group->field('id', array('name'=>$d["name"]));
            } else {
                $user = ClassRegistry::init("User");
                $d["ugid"] = $user->field('id', array('userid'=>$d["name"]));
            }
            if(!$this->save($d)) {
                throw new BeditaException(__("Error saving permissions", true), 
                    "obj: $objectId - permissions: ". var_export($perms, true));
                ;
            }
        }
    }
    
    /**
     * Remove all object permissions.
     *
     * @param integer $objectId     object ID
     */
    public function removeAll($objectId) {
        if(!$this->deleteAll(array("object_id" => $objectId), false))
            throw new BeditaException(__("Error removing permissions", true), "object id: $objectId");
    }   
    
    /**
     * Updates/replaces object permissions
     *
     * @param integer $objId    object id
     * @param array $perms      array like (array("flag"=>1, "switch" => "group", "name" => "guest"), array(...))
     */
    public function replace($objectId, $perms) {
        $this->removeAll($objectId);
        $this->add($objectId, $perms);
    }


    /**
     * remove old permissions on $groupId and add new $perms
     *
     * @param  int $groupId
     * @param  array $perms array like (array("flag"=> 1, "object_id"), array(...))
     */
    public function replaceGroupPerms($groupId, array $perms) {
        // remove all group permissions
        if(!$this->deleteAll(array('ugid' => $groupId, 'switch' => 'group'), false)) {
            throw new BeditaException(__("Error removing permissions for group", true) . " $groupId");
        }
        foreach ($perms as $p) {
            $p['ugid'] = $groupId;
            $p['switch'] = 'group';
            $this->create();
            if (!$this->save($p)) {
                throw new BeditaException(__("Error saving permissions for group", true), array($p));
            }
        }
    }
    
    /**
     * Is object ($objectId) writable by user?
     *
     * @param integer $objectId
     * @param array $userData user data, like array("id" => .., "userid" => ..., "groups" => array("administrator", "frontend",...))
     * @param $perms permission array defined like in checkPermissionByUser() call
     *               if it's defined use it else get permission by $objectId
     * @return boolean, true if it's writable
     */
    public function isWritable($objectId, array &$userData, $perms = array()) {
        // administrator can always write....
        if(!empty($userData['groups']) && in_array("administrator",$userData['groups'])) {
            return true;        
        }
        if (empty($perms)) {
            $perms = $this->isPermissionSet($objectId, Configure::read("objectPermissions.write"));
        }
        return $this->checkPermissionByUser($perms, $userData);
    }

    /**
     * Is object ($objectId) forbidden to user?
     * Backend only (check backend_private permission)
     *
     * @param integer $objectId
     * @param array $userData user data, like array("id" => .., "userid" => ..., "groups" => array("administrator", "frontend",...))
     * @return boolean, true if it's forbidden false if it's allowed
     */
    public function isForbidden($objectId, array &$userData) {
        // no private objects for administrator
        if (!BACKEND_APP || ( !empty($userData['groups']) && in_array("administrator", $userData['groups'])) ) {
            return false;
        }

        $forbidden = false;
        $privatePermission = Configure::read("objectPermissions.backend_private");

        // check perms on main object ($objectId)
        $perms = $this->isPermissionSet($objectId, $privatePermission);
        $forbidden = !$this->checkPermissionByUser($perms, $userData);
        if ($forbidden) {
            return true;
        }

        // check if some branch parent is allowed, if so object is not forbidden
        $parentsPath = ClassRegistry::init('Tree')->find('list', array(
            'fields' => array('parent_path'),
            'conditions' => array('id' => $objectId)
        ));

        if (!empty($parentsPath)) {
            foreach ($parentsPath as $path) {
                $path = trim($path, '/');
                $pathArr = explode('/', $path);
                $branchAllowed = array();
                foreach ($pathArr as $parentId) {
                    $perms = $this->isPermissionSet($parentId, $privatePermission);
                    $branchAllowed[] = $this->checkPermissionByUser($perms, $userData);
                }

                if (!in_array(false, $branchAllowed)) {
                    $forbidden = false;
                    break;
                } else {
                    $forbidden = true;
                }
            }
        }

        return $forbidden;
    }

    /**
     * Is object ($objectId) accessible by user in frontend?
     * 
     * @param $objectId
     * @param $userData  user data, like array("id" => .., "userid" => ..., "groups" => array("administrator", "frontend",...))
     * @param $perms permission array defined like in checkPermissionByUser() call
     *               if it's defined use it else get permission by $objectId
     * @return boolean, true if it's accessible
     */
    public function isAccessibleByFrontend($objectId, array &$userData, $perms = array()) {
        if (empty($perms)) {
            $perms = $this->isPermissionSet($objectId, array(
                Configure::read("objectPermissions.frontend_access_with_block"),
                Configure::read("objectPermissions.frontend_access_without_block")
            ));
        }
        return $this->checkPermissionByUser($perms, $userData);
    }
    
    /**
     * Return frontend level access to an object
     *
     * Possible returned values are:
     *
     * * 'free' if the object has not frontend_access perms
     * * 'denied' if the object isn't accessible (frontend_access_with_block perms set and user groups haven't that permission on that object)
     * * 'partial' if the object is accessible in preview (frontend_access_without_block perms set and user groups haven't that permission on that object)
     * * 'full' if the object has perms and user groups have that permission on that object
     *
     * @param int $objectId
     * @param array &$userData user data as
     *                         ```
     *                         array(
     *                             'id' => ..,
     *                             'userid' => ...,
     *                             'groups' => array('administrator', 'frontend',...)
     *                         )
     *                         ```
     * @return string
     */
    public function frontendAccess($objectId, array &$userData = array()) {
        $accessWithBlock = Configure::read('objectPermissions.frontend_access_with_block');
        $accessWithoutBlock = Configure::read('objectPermissions.frontend_access_without_block');
        $perms = $this->isPermissionSet($objectId, array($accessWithBlock, $accessWithoutBlock));

        // full access because no perms are set
        if (empty($perms)) {
            return 'free';
        }

        if (!empty($userData)) {
            // full access => one perm for user group
            if ($this->checkPermissionByUser($perms, $userData)) {
                return 'full';
            }
        }

        $flags = Set::extract('/Permission/flag', $perms);

        // access denied => object has at least one perm 'frontend_access_with_block'
        if (in_array($accessWithBlock, $flags)) {
            return 'denied';
        }
        // partial access => object has at least one perm 'frontend_access_without_block'
        return 'partial';
    }

    /**
     * check if user or user groups are in $perms array
     * 
     * @param $perms permission array like return from find("all)
     *                      array(
     *                          0 => array("Permission" => array(...), "User" => array(...), "Group" => array(...)),
     *                          1 => ....
     *                      )
     * @param $userData user data, like array("id" => .., "userid" => ..., "groups" => array("administrator", "frontend",...))
     * @return boolean (true if user have permission false otherwise)
     */
    public function checkPermissionByUser($perms=array(), array &$userData) {
        if(empty($perms))
            return true;

        foreach ($perms as $p) {
            if(!empty($p['User']) && $userData['id'] == $p['Permission']['ugid']) {
                return true;
            }
            if(!empty($p['Group']) && !empty($userData['groups']) && in_array($p['Group']['name'], $userData['groups'])) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * check if a permission over an object is set
     *
     * @param integer $objectId
     * @param array|integer $flag permission
     * @return array of perms with users and groups or false if no permission is setted
     */
    public function isPermissionSet($objectId, $flag) {
        if (!is_array($flag)) {
            $flag = array($flag);
        }
        $result = $this->load($objectId, $flag);
        $ret = (!empty($result)) ? $result : false;
        return $ret;
    }
    
    /**
     * Delete a permit for an object
     *
     * @param integer $id       object ID
     * @param array $perms      array like (array("flag"=>1, "switch" => "group", "name" => "guest"), array(...))
     */
    public function remove($objectId, $perms) {

        foreach ($perms as $p) {
            $conditions = array("object_id" => $objectId, "switch" => $p["switch"]);
            if (isset($p["flag"])) {
                $conditions["flag"] = $p["flag"];
            }
            if($p["switch"] == "group") {
                $group = ClassRegistry::init("Group");
                $conditions["ugid"] = $group->field('id', array('name' => $p["name"]));
            } else {
                $user = ClassRegistry::init("User");
                $conditions["ugid"] = $user->field('id', array('userid' => $p["name"]));
            }
            if(!$this->deleteAll($conditions, false))
                throw new BeditaException(__("Error removing permissions", true), "object id: $objectId");
        }
    }   

    /**
     * Load all object permissions, using object cache, selecting one or more flag value
     * 
     * @param integer $objectId
     * @param mixed $flag array or string with flag permission to search
     * @return array (permissions)
     */
    public function load($objectId, $flag = array()) {
        $perms = false;
        if ($this->BeObjectCache) {
            $options = array();
            $perms = $this->BeObjectCache->read($objectId, $options, 'perms');
        }
        if (!is_array($flag)) {
            $flag = array($flag);
        }
        if ($perms === false) {
            $conditions = array('object_id' => $objectId);
            if (!$this->BeObjectCache && !empty($flag)) {
                $conditions['flag'] = $flag;
            }
            $perms = $this->find('all', array(
                'conditions' => $conditions,
            ));
            $this->cleanupPermissionData($perms);
            if ($this->BeObjectCache) {
                $this->BeObjectCache->write($objectId, $options, $perms, 'perms');
            }
        }
        // search $flag inside $perms
        if (!empty($perms) && !empty($flag) && $this->BeObjectCache) {
            foreach ($perms as $i => $p) {
                if (!in_array($p['Permission']['flag'], $flag)) {
                    unset($perms[$i]);
                }
            }
        }
        return $perms;
    }

    private function cleanupPermissionData(array &$perms) {
        foreach ($perms as $i => &$p) {
            if (!empty($p['Group']['name'])) {
                $p['Group'] = array_intersect_key(
                    $p['Group'],
                    array_flip(array('name'))
                );
                unset($p['User']);
            } else if (!empty($p['User']['userid'])) {
                $p['User'] = array_intersect_key(
                    $p['User'],
                    array_flip(array('userid'))
                );
                unset($p['Group']);
            }
        }
    }

    /**
     * passed an array of BEdita objects add 'count_permission' key
     * with the number of permissions applied to objects
     *
     * @param  array $objects
     * @param  array $options
     *              - flag: if specified count permission with that flag
     * @return array $objects with added 'count_permission' key
     */
    public function countPermissions(array $objects, array $options) {
        foreach ($objects as &$obj) {
            $conditions = array('object_id' => $obj['id']);
            if (isset($options['flag'])) {
                $conditions['flag'] = $options['flag'];
            }
            $obj['num_of_permission'] = $this->find('count', array(
                'conditions' => $conditions
            ));
        }
        return $objects;
    }

    /**
     * Return information about frontend not accessible related objects to $objectId
     *
     * If $options['count'] = false (default) it returns a list of object ids with permission 'frontend_access_with_block'
     * related to main object $objectId.
     * Passing also $options['relation'] it filters by relation name
     *
     * If $options['count'] = true it returns an array of count of objects with permission 'frontend_access_with_block'
     * related to main object $objectId and grouped by relation name
     *
     * Example:
     * ```
     * array(
     *     'attach' => 14,
     *     'seealso' => 7
     * )
     * ```
     *
     * If $user['groups'] is specified then it tests related objects against user groups and return a list
     * without objects allowed to user or a count of objects not allowed to user
     *
     * @param int $objectId the main object id
     * @param string $relation the relation name
     * @param array $user the user data on which check perms
     * @return array
     */
    public function relatedObjectsNotAccessibile($objectId, array $options = array(), array $user = array()) {
        $options += array(
            'relation' => null,
            'count' => false,
            'objectConditions' => array(),
        );

        $objectsForbidden = false;
        if ($this->BeObjectCache) {
            $objectsForbidden = $this->BeObjectCache->read($objectId, $options, 'related-perms');
        }

        if (!empty($user) || $objectsForbidden === false) {
            $conditions = array(
                'Permission.flag' => Configure::read('objectPermissions.frontend_access_with_block'),
                'Permission.switch' => 'group',
                'ObjectRelation.id' => $objectId
            );
            if (!empty($options['relation'])) {
                $conditions['ObjectRelation.switch'] = $options['relation'];
            }

            if ($options['count']) {
                $findType = 'all';
                $fields = array('COUNT(DISTINCT(Permission.object_id)) as count, ObjectRelation.switch');
                $group = 'ObjectRelation.switch';
            } else {
                $findType = 'list';
                $fields = array('Permission.id', 'Permission.object_id');
                $group = 'Permission.object_id';
            }

            $joins = array(
                array(
                    'table' => 'object_relations',
                    'alias' => 'ObjectRelation',
                    'type' => 'inner',
                    'conditions' => array(
                        'Permission.object_id = ObjectRelation.object_id',
                    )
                )
            );

            // if status is defined add join with objects
            if (!empty($options['objectConditions'])) {
                $joins[] = array(
                    'table' => 'objects',
                    'alias' => 'BEObject',
                    'type' => 'inner',
                    'conditions' => array_merge(
                        array('Permission.object_id = BEObject.id'),
                        $options['objectConditions']
                    )
                );
            }
        }

        if ($objectsForbidden === false) {
            $objectsForbidden = $this->find($findType, array(
                'fields' => $fields,
                'conditions' => $conditions,
                'joins' => $joins,
                'group' => $group
            ));
            if ($this->BeObjectCache) {
                $this->BeObjectCache->write($objectId, $options, $objectsForbidden, 'related-perms');
            }
        }

        // get objects allowed to user
        if (!empty($user) && !empty($objectsForbidden)) {
            if (!empty($user['groupsIds'])) {
                $conditions['Permission.ugid'] = $user['groupsIds'];
            } elseif (!empty($user['groups'])) {
                $groupList = ClassRegistry::init('Group')->getList(array(
                    'Group.name' => $user['groups']
                ));
                $conditions['Permission.ugid'] = array_keys($groupList);
            }
            $objectsAllowed = $this->find($findType, array(
                'fields' => $fields,
                'conditions' => $conditions,
                'joins' => $joins,
                'group' => $group
            ));
        }
        if (empty($objectsAllowed)) {
            $objectsAllowed = array();
        }

        if ($options['count']) {
            $relationsCount = array();
            $objectsAllowed = Set::combine($objectsAllowed, '{n}.ObjectRelation.switch', '{n}.{n}.count');
            foreach ($objectsForbidden as $detail) {
                $count = $detail[0]['count'];
                $switch = $detail['ObjectRelation']['switch'];
                if (!empty($objectsAllowed[$switch])) {
                    $count -= $objectsAllowed[$switch][0];
                }
                $relationsCount[$detail['ObjectRelation']['switch']] = $count;
            }
            return $relationsCount;
        } else {
            $objectsForbidden = array_diff(
                array_values($objectsForbidden),
                array_values($objectsAllowed)
            );
        }

        return $objectsForbidden;
    }

    /**
     * Return true if object $objectId and its parents are accessible i.e. for $user
     * 'Accessible' means without 'frontend_access_with_block' permission set
     *
     * $options params are:
     * - 'status' the status of parents to check
     * - 'area_id' the parents publication id
     * - 'stopIfMissingParents' true (default) to stop and return not valid if $objectId haven't parents and it isn't a publication
     *
     * @param int $objectId the object id
     * @param array $options
     * @param array $user the user data
     * @return boolean
     */
    public function isObjectsAndParentsAccessible($objectId, array $options = array(), array $user = array()) {
        $access = $this->frontendAccess($objectId, $user);
        if($access == 'denied') {
            return false;
        }
        $userGroups = !empty($user['groupsIds']) ? $user['groupsIds'] : array();
        return $this->objectParentsAccessible($objectId, $options, $userGroups);
    }

   /**
     * Return true if object $objectId parents are accessible for a user
     * 'Accessible' means without 'frontend_access_with_block' permission set
     *
     * $options params are:
     *
     * - 'status' the status of parents to check
     * - 'area_id' the parents publication id
     * - 'stopIfMissingParents' true (default) to stop and return not valid if $objectId haven't parents and it isn't a publication
     *
     * @param int $objectId the object id
     * @param array $options
     * @param array $userGroups array of user groups ids
     * @return boolean
     */
    public function objectParentsAccessible($objectId, array $options = array(), array $userGroups = array()) {
        $options += array(
            'status' => array(),
            'area_id' => null,
            'stopIfMissingParents' => true
        );
        $tree = ClassRegistry::init('Tree');
        $parents = $tree->getParents($objectId, $options['area_id'], $options['status']);
        // no parents
        if (empty($parents) && $options['stopIfMissingParents']) {
            $objectTypeId = ClassRegistry::init('BEObject')->findObjectTypeId($objectId);
            $areaObjectTypeId = Configure::read('objectTypes.area.id');
            // return false if object type is not area or if 'area_id' was passed and it's different from $objectId
            if ($objectTypeId != $areaObjectTypeId || (!empty($options['area_id']) && $objectId != $options['area_id'])) {
                return false;
            } else {
                return true;
            }
        }

        $flag = Configure::read('objectPermissions.frontend_access_with_block');
        foreach ($parents as $id) {
            $perms = $this->load($id, $flag);
            $groupsAllowed = array();
            foreach ($perms as $p) {
                $groupsAllowed[] = $p['Permission']['ugid'];
            }
            $permsAllowed = array_intersect($groupsAllowed, $userGroups);
            if (!empty($groupsAllowed) && empty($permsAllowed)) {
                return false;
            }
        }
        return true;
    }
}
BEdita API doc | 4-cactus API documentation generated by ApiGen