Announcement

Collapse
No announcement yet.

Portal User and Assigned User.

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

  • Portal User and Assigned User.

    The Article entity has been created.
    Portal users are linked to their Contact.

    When a portal user creates a new Article, he must manually specify the Assigned User, as this field is required.
    To do this, in the "Assigned User" selection, switch from "Active" to "All" and find your Contact in the list. It is very uncomfortable.
    I wanted to make an automatic selection of the Assigned User. Tried formulas like this but they don't work. Help me, please.


    assignedUser = ifThen(
    user.contact != null,
    entity\addLinkMultipleId('assignedUser', 'user.contactId')
    );

    ----------------------------------------------------

    ifThen(
    user.contact != null,
    entity\addLinkMultipleId('assignedUser', 'user.contactId')
    );
    -----------------------------------------------------

    ifThen(createdBy.isPortalUser, assignedUserId = 'user.contactId');

    ----------------------------------------------------------

    ifThen(createdBy.isPortalUser, assignedUserId = 'contactId');

  • #2
    Hello,
    Portal user doesn't have such permission. Portal is developed for customers to give them tiny access into the system.
    You can try this formula, but I'm not sure it will help:
    Code:
    ifThen(
        entity\isNew() && createdBy.type == 'Portal',
        assignedUserId = createdById
    );

    Comment


    • #3
      Unfortunately, it didn't help.

      Comment


      • #4
        Replace createdBy.type == 'Portal' with createdBy.type == 'portal' and try again.

        Comment


        • #5
          Doesn't work either. The required assignedUser field is not autocompleted. Maybe I need to use Hooks?







          Comment


          • #6
            Maybe I need to use Hooks?
            Try this to write a hook to automatically fill in the value of "assignedUserId",with the value of the current user's id (if it's a portal user), when creating a new KnowledgeBaseArticle entity:

            1) Create a custom hook class for the KnowledgeBaseArticle entity:

            custom/Espo/Custom/Hooks/KnowledgeBaseArticle/KnowledgeBaseArticleHooks.php
            PHP Code:
            namespace Espo\Custom\Hooks\KnowledgeBaseArticle;

            use 
            Espo\ORM\Entity;
            use \
            Espo\Core\Exceptions\BadRequest;

            class 
            KnowledgeBaseArticleHooks extends \Espo\Core\Hooks\Base
            {

                public function 
            beforeSave(Entity $entity, array $options=array())
                {
                    
            // get the current user object
                    
            $authorObject $this->getUser();
                    
            // make sure that the user is a Portal User
                    
            if($authorObject->get('type') === "portal") {
                        
            // fill the assigned user id value of the article with the author's id
                        
            $entity->set('assignedUserId'$authorObject->get('id'));
                    }

                }

            2) Clear cache and rebuild
            Last edited by telecastg; 10-12-2020, 05:55 PM.

            Comment


            • #7
              Similarly, I created a file ArticleHooks.php in this directory

              custom/Espo/Custom/Hooks/Article/ArticleHooks.php




              <?php
              namespace Espo\Custom\Hooks\Article;

              use Espo\ORM\Entity;
              use \Espo\Core\Exceptions\BadRequest;

              class ArticleHooks extends \Espo\Core\Hooks\Base
              {

              public function beforeSave(Entity $entity, array $options=array())
              {
              // get the current user object
              $authorObject = $this->getUser();
              // make sure that the user is a Portal User
              if($authorObject->get('type') === "portal") {
              // fill the assigned user id value of the article with the author's id
              $entity->set('assignedUserId', $authorObject->get('id');
              }

              }
              }



              But after clearing the cache, I cannot make any changes in other entities, such as Contact and User Portal. Shows me an error
              http://prntscr.com/uxn65w

              By the way, if you have knowledge in this area, please help me on another topic with Hooks
              https://forum.espocrm.com/forum/deve...tween-contacts


              Last edited by sobolevfff; 10-12-2020, 11:06 AM.

              Comment


              • #8
                But after clearing the cache, I cannot make any changes in other entities, such as Contact and User Portal. Shows me an error
                $entity->set('assignedUserId', $authorObject->get('id');
                should be: $entity->set('assignedUserId', $authorObject->get('id')); there was a comma missing in the original posting, which I corrected it.

                Make the correction and rebuild to try again.

                Your hook only affects the "Article" entity (I assume that "Article" is a custom entity and that it has a property (field) "assignedUserId" ? ) but has nothing to do with other entities like Contact or User. If the error message comes again, check the error log at data/logs to see what is the actual problem and correct it.
                Last edited by telecastg; 10-12-2020, 05:59 PM.

                Comment


                • #9
                  I checked the log file, there was an error with a bracket ")", then with a comma ",". I fixed the code and the error was gone, but the code still does not work, it requires to select the Assigned User.

                  <?php
                  namespace Espo\Custom\Hooks\Article;

                  use Espo\ORM\Entity;
                  use \Espo\Core\Exceptions\BadRequest;

                  class ArticleHooks extends \Espo\Core\Hooks\Base
                  {

                  public function beforeSave(Entity $entity, array $options=array())
                  {
                  // get the current user object
                  $authorObject = $this->getUser();
                  // make sure that the user is a Portal User
                  if($authorObject->get('type') === "portal") {
                  // fill the assigned user id value of the article with the author's id
                  $authorObject->get('id');
                  $entity->set('assignedUserId', $authorObject);
                  }

                  }
                  }



                  Probably the mistake is that I need to find the Contact ID, not the Portal User ID because in the Assigned user field is selected from the Contacts?
                  Last edited by sobolevfff; 10-12-2020, 07:29 PM.

                  Comment


                  • #10
                    <?php
                    namespace Espo\Custom\Hooks\Article;

                    use Espo\ORM\Entity;
                    use \Espo\Core\Exceptions\BadRequest;

                    class ArticleHooks extends \Espo\Core\Hooks\Base
                    {

                    public function beforeSave(Entity $entity, array $options=array())
                    {
                    // get the current user object
                    $authorObject = $this->getUser();
                    // make sure that the user is a Portal User
                    if($authorObject->get('type') === "portal") {
                    // fill the assigned user id value of the article with the author's id
                    $contact = $entityManager->getRepository('Contact')->get($contactId);

                    $entity->set('assignedUserId', $contact);
                    }

                    }
                    }


                    That doesn't work either.

                    Comment


                    • #11
                      Probably the mistake is that I need to find the Contact ID, not the Portal User ID because in the Assigned user field is selected from the Contacts?
                      The assignedUserId is supposed to contain a User id not a Contact id

                      $authorObject->get('id');
                      $entity->set('assignedUserId', $authorObject);
                      is wrong, check the example, it is supposed to be:
                      $entity->set('assignedUserId', $authorObject->get('id'));
                      It is important to understand the code, what you are doing in your code is trying to load the 'assignedUserId' field with an object ($authorObject) NOT with the author's id value which is what you get with the statement $authorObject->get('id')

                      it requires to select the Assigned User.
                      Where is the assignedUserId field set as required ?. Unset the "required" flag so the front end process is not interrupted.

                      The afterSave hook will work AFTER the record has been saved so if you have the condition "required" as true before the record can be even be saved, the afterSave hook never takes place.
                      Last edited by telecastg; 10-12-2020, 08:41 PM.

                      Comment


                      • #12
                        Hellio,
                        i think is so :

                        $user = $this->getUser();
                        if ( $user->get('isPortalUser') ) {
                        $entity->set([ 'assignedUserId' => $user->id ]);
                        }

                        Comment


                        • #13
                          I made the "required" flag optional.

                          Now both versions item and telecastg of the code have the same error:

                          [2020-10-12 21:41:56] Espo.ERROR: (403) Assignment permission failure.; POST /crm/api/v1/portal-access/5f7b0ed7309e1fe3f/Article; line: 938, file: /home/assssssssssss/public_html/crm/application/Espo/Services/Record.php [] []

                          http://prntscr.com/uy1kza

                          Comment


                          • #14
                            Hi,
                            i have see, so portalUser can not assign (out-of-box role) .. it's really a very perfect solution by Yuri
                            but always solution with espoCRM

                            on afterSave hook :

                            check if entity->isNew and "isPortalUser"
                            $pdo = $this->getEntityManager()->getPDO();
                            $sql = "update entity set .... where id = entity->id";
                            $sth = $pdo->prepare($sql);
                            $sth->execute();




                            Last edited by item; 10-12-2020, 11:56 PM.

                            Comment


                            • telecastg
                              telecastg commented
                              Editing a comment
                              Excellent solution item , bypassing the ORM and ACL to update the database directly.

                              sobolevfff since you are going to be updating the database table directly, you need to address the actual db table name and actual field name, not the ORM references, so the sql statement should be:

                              $sql = "update `article` set `article`.assigned_user_id = '".this.getUser()->id."' where `article`.id = '".$entity->id."'";
                              Last edited by telecastg; 10-13-2020, 02:52 AM.

                          • #15
                            <?php
                            namespace Espo\Custom\Hooks\Article;
                            use Espo\ORM\Entity;
                            use \Espo\Core\Exceptions\BadRequest;
                            class ArticleHooks extends \Espo\Core\Hooks\Base
                            {
                            public function beforeSave(Entity $entity, array $options=array())
                            {
                            // get the current user object
                            $authorObject = $this->getUser();
                            // make sure that the user is a Portal User
                            if($entity->isNew() && $authorObject->get('type') === "portal") {
                            $pdo = $this->getEntityManager()->getPDO();
                            $sql = "update `article` set `article`.assigned_user_id = '".this.getUser()->id."' where `article`.id = '".$entity->id."'";
                            $sth = $pdo->prepare($sql);
                            $sth->execute();
                            }
                            }
                            }

                            Good day! I am trying code like this. Tried afterSave and beforeSave and gives the same error

                            [2020-10-13 07:41:05] Espo.ERROR: (403) Ошибка разрешения на назначение.; POST / crm / api / v1 / portal-access / 5f7b0ed7309e1fe3f / Article; строка: 938, файл: /home/aaaaaaa/public_html/crm/application/Espo/Services/Record.php [] []

                            MySql Table
                            http://prntscr.com/uy9xn4



                            Last edited by sobolevfff; 10-13-2020, 09:04 AM.

                            Comment


                            • telecastg
                              telecastg commented
                              Editing a comment
                              It looks like bypassing the ORM was not enough to bypass ACL and because portal users have very limited access by design (which is absolutely correct) the API call throws an "Unauthorized" error. Unfortunately I don't know how to overcome this.

                              We don't have the advanced pack installed but if you do, you might try to create a workflow to do the automatic assignment of the article.
                          Working...
                          X