Records Table: add a default filter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wtconseil
    Active Community Member
    • Apr 2015
    • 335

    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

  • tanya
    Senior Member
    • Jun 2014
    • 4308

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

    Comment

    • wtconseil
      Active Community Member
      • Apr 2015
      • 335

      #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

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #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

        Comment

        • wtconseil
          Active Community Member
          • Apr 2015
          • 335

          #5
          You're completely right, thanks!

          Comment

          • wtconseil
            Active Community Member
            • Apr 2015
            • 335

            #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 $user, Entity $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

            • tanya
              Senior Member
              • Jun 2014
              • 4308

              #7
              try this

              Code:
              $this->getAclManager()->getImplementation('Document')->checkAccessToFinancialConfidentiality($user);

              Comment

              • wtconseil
                Active Community Member
                • Apr 2015
                • 335

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

                Comment

                • bdcitsolutions
                  Member
                  • Apr 2018
                  • 79

                  #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

                  • tanya
                    Senior Member
                    • Jun 2014
                    • 4308

                    #10
                    EspoCRM – Open Source CRM Application. Contribute to espocrm/espocrm development by creating an account on GitHub.


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

                    Comment

                    • bdcitsolutions
                      Member
                      • Apr 2018
                      • 79

                      #11
                      Originally posted by tanya
                      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

                      • tanya
                        Senior Member
                        • Jun 2014
                        • 4308

                        #12
                        _getName in custom/Espo/Custom/Entities/ your event classes
                        Hi, In my Entity Opportunities is an 1:n Relation to a custom entity called &quot;Kredite&quot;. The Entity &quot;Kredite&quot; has an not storable field

                        Comment

                        • bandtank
                          Active Community Member
                          • Mar 2017
                          • 379

                          #13
                          Does anyone know how to do this in 7.2? So much has changed. I'm not sure if the same methods are useful anymore. I would like to disable read access for all events on the calendar that have a boolean called private set to True. Meetings, calls, tasks, and many custom entities of type event would use this feature. Ideally, I'd also be able to override the name of the event to 'Private'.

                          Select Manager is deprecated. Select Builder is supposed to be used now, but I can't figure out how to use it. This page says to create a class, which I did, but then what? Somehow the calendar needs to use the class.
                          Last edited by bandtank; 10-10-2022, 11:04 PM.

                          Comment

                          Working...