Announcement

Collapse
No announcement yet.

Records Table: add a default filter

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Records Table: add a default filter

    Hi there

    in Document entity, i add a boolean attribute "financialConfidentiality".
    I want to control the read access to the "documents" having this attribut set. It will be only accessible to users having the good role.

    I'm working on an ACL to restrict "read" access...
    cf : http://forum.espocrm.com/forum/bug-r...heckentityread

    But i would like to filter the default record table content that is displayed when clicking on the menu "Documents".
    By default, all documents are listed... I would like to add my custom code to add a where condition (if my current user has a given role and the row has attribute financialConfidentiality: true, it can be displayed).

    i have already used SelectManager to add custom Filter (on the left part of the search field).

    Here, i would like to add this default behaviour on the record table, whatever a custom filter is set or not.

    Which method should i override in custom folder?
    Thanks a lot


  • #2
    Hello
    I think you need to override list view and define own getSearchDefaultData method
    Job Offers and Requests

    Find Developer
    Find Customer

    Comment


    • #3
      The following implementation is working fine but i don't know if it's suitable regarding how EspoCRM was designed.
      Your feedback is welcomed :-)

      So, to filter all the collection, whatever the given filter is set (no filter, or custom filter), i implement this custom SelectManager...
      the objectif is to add everywhere

      $result['whereClause'][] = array(
      'financialConfidentiality' => false
      );

      to filter the rows that should not be displayed...

      I override the method access i don't know if there is a better one i should ?

      Thanks and have a good day!

      custom/Espo/Custom/SelectManagers/Document.php

      PHP Code:
      <?php

      namespace Espo\Custom\SelectManagers;

      class 
      Document extends \Espo\Modules\Crm\SelectManagers\Document {

        protected function 
      access(&$result) {

          
      $user $this->user;

          if (
      $this->getSeed()->hasAttribute('financialConfidentiality')) {
            
      $roleList = [];
            
      $allowed $user->isAdmin();

            foreach (
      $user->get('roles') as $role)
              
      $roleList[] = $role;

            foreach(
      $roleList as $role) {
              if (
      $role->get('name') == 'AdminDocumentFinancier') {
                
      $GLOBALS['log']->info('[Custom] SelectManager : Document => user '.$user->get('name').' has role AdminDocumentFinancier => OK');
                
      $allowed true;
              }
            }

            if(!
      $allowed) {
              
      $GLOBALS['log']->info('[Custom] SelectManager : Document => missing role AdminDocumentFinancier for user '.$user->get('name').' -> filtering...');
              
      $result['whereClause'][] = array(
                  
      'financialConfidentiality' => false
              
      );
            }
          }
          
      parent::access($result);
        }
      }

      Comment


      • #4
        I propose you to create a method named like checkAccessToFinancialConfidentiality(EntityUser $user) in Acl part (Espo\Custom\Acl\Document)

        and use in SelectManager $this->getAcl()->checkAccessToFinancialConfidentiality($this->getUser()) instead of $allowed

        this is the best way to control access
        Job Offers and Requests

        Find Developer
        Find Customer

        Comment


        • #5
          You're completely right, thanks!

          Comment


          • #6
            Hmmm, i have an issue.
            I think that on my custom Document SelectManager, it doesn't see my own method checkAccessToFinancialConfidentiality setup in my Custom Document ACL

            Custom ACL Document.php
            PHP Code:
            <?php
            namespace Espo\Custom\Acl;
            use \
            Espo\Entities\User as EntityUser;
            use \
            Espo\ORM\Entity;
            use \
            Espo\Core\Exceptions\Forbidden;

            class 
            Document extends \Espo\Core\Acl\Base {

              public function 
            checkEntityRead(EntityUser $userEntity $entity$data) {

                if (
            $entity->get('financialConfidentiality')) {
                  return 
            $this->checkAccessToFinancialConfidentiality($user);
                }
                return 
            true;
              }

              public function 
            checkAccessToFinancialConfidentiality(EntityUser $user) {
                if(
            $user->isAdmin()) {
                  return 
            true;
                }

                
            $roleList = [];
                foreach (
            $user->get('roles') as $role)
                  
            $roleList[] = $role;

                foreach(
            $roleList as $role) {
                  if (
            $role->get('name') == 'AdminDocumentFinancier') {
                    return 
            true;
                  }
                }
                return 
            false;
              }
            }
            Custom SelectManager Document.php
            PHP Code:
            <?php

            namespace Espo\Custom\SelectManagers;

            class 
            Document extends \Espo\Modules\Crm\SelectManagers\Document {

              protected function 
            access(&$result) {
                
            $user $this->user;

                if (
            $this->getSeed()->hasAttribute('financialConfidentiality')) {  
                  if(! 
            $this->getAcl()->checkAccessToFinancialConfidentiality($user) ) {
                    
            $result['whereClause'][] = array(
                        
            'financialConfidentiality' => false
                    
            );
                  }
                }
                
            parent::access($result);
              }
            }


            I have the following error "Bad server Response" and here is the log
            Fatal error: Call to undefined method Espo\Core\Acl::checkAccessToFinancialConfidentiali ty() in /var/www/html/custom/Espo/Custom/SelectManagers/Document.php on line 13

            I rebuilt + clear cache

            The method checkAccessToFinancialConfidentiality is public and is working when used in Custom ACL.
            It doest not work when i try to use it through $this->getAcl()->checkAccessToFinancialConfidentiality($user)

            does this synthax use the Custom ACL or only the default ACL ?

            maybe it doesn't follow the interface that is implemented?

            Thanks for your help :-)

            Comment


            • #7
              try this

              Code:
              $this->getAclManager()->getImplementation('Document')->checkAccessToFinancialConfidentiality($user);
              Job Offers and Requests

              Find Developer
              Find Customer

              Comment


              • #8
                Perfectly working!
                Thanks for your quick answer and your 1st-class quality support here !!!

                Comment


                • #9
                  Hi,

                  we've implemented this for our event entities and everything works great! Our users can now check "private" and their events are hidden from others in the list view and the users get an "access denied" if they want to open the events in the calendar timeline.

                  Is there also a chance to hide or remove the name of the event in the calendar timeline if "private" is checked?

                  Best,

                  Daniel

                  Comment


                  • #10
                    https://github.com/espocrm/espocrm/b...Activities.php

                    In other topic you want to override access method in SelectMeneger. It will be be used here as well.
                    Or override service Activities.
                    Job Offers and Requests

                    Find Developer
                    Find Customer

                    Comment


                    • #11
                      Originally posted by tanya View Post
                      https://github.com/espocrm/espocrm/b...Activities.php

                      In other topic you want to override access method in SelectMeneger. It will be be used here as well.
                      Or override service Activities.
                      In Services/Activites.php I get only the SQL query for the event entities, not the events themselves. Is there any chance to poll through all the events while creating the calendar view and remove their name if "private" is set true in it?

                      Comment


                      • #12
                        _getName in custom/Espo/Custom/Entities/ your event classes
                        https://forum.espocrm.com/forum/deve...in-an-relation
                        Job Offers and Requests

                        Find Developer
                        Find Customer

                        Comment

                        Working...
                        X