]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/UserBundle/Entity/User.php
WIP
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
index ff658ca5f70f2d0e048c8afefe2c3567c3378b4a..2b73e344a75aea4038df0a648474e0254ac3ac45 100644 (file)
@@ -15,6 +15,8 @@ use Symfony\Component\Security\Core\User\UserInterface;
 use Wallabag\ApiBundle\Entity\Client;
 use Wallabag\CoreBundle\Entity\Config;
 use Wallabag\CoreBundle\Entity\Entry;
+use Wallabag\GroupBundle\Entity\Group;
+use Wallabag\GroupBundle\Entity\UserGroup;
 
 /**
  * User.
@@ -98,13 +100,11 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
     private $authCode;
 
     /**
-     * @ORM\ManyToMany(targetEntity="Wallabag\GroupBundle\Entity\Group", inversedBy="users", cascade={"persist"})
-     * @ORM\JoinTable(name="user_group",
-     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
-     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
-     * )
+     * @var ArrayCollection
+     *
+     * @ORM\OneToMany(targetEntity="Wallabag\GroupBundle\Entity\UserGroup", mappedBy="user", cascade={"persist", "remove"})
      */
-    protected $groups;
+    protected $userGroups;
 
     /**
      * @var bool Enabled yes/no
@@ -136,6 +136,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
     {
         parent::__construct();
         $this->entries = new ArrayCollection();
+        $this->userGroups = new ArrayCollection();
         $this->roles = ['ROLE_USER'];
     }
 
@@ -323,11 +324,65 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
     }
 
     /**
-     * @param string $name
+     * @param Group $group
+     * @return UserGroup
+     */
+    public function getUserGroupFromGroup(Group $group)
+    {
+        foreach ($this->userGroups as $userGroup) {
+            if ($userGroup->getGroup() == $group) {
+                return $userGroup;
+            }
+        }
+        return null;
+    }
+
+
+    /**
+     * @param Group $group
+     * @param $role
+     */
+    public function setGroupRole(Group $group, $role)
+    {
+        if ($userGroup = $this->getUserGroupFromGroup($group)) {
+            $userGroup->setRole($role);
+        }
+    }
+
+    /**
+     * @param Group $group
+     * @return int
+     */
+    public function getGroupRoleForUser(Group $group)
+    {
+        if ($userGroup = $this->getUserGroupFromGroup($group)) {
+            return $userGroup->getRole();
+        }
+        return 0;
+    }
+
+    /**
+     * @param Group $group
      * @return bool
      */
-    public function hasGroup($name = '')
+    public function inGroup(Group $group)
     {
-        return in_array($name, $this->getGroupNames());
+        if ($group::ACCESS_REQUEST === $group->getAcceptSystem()) {
+            $userGroup = $this->getUserGroupFromGroup($group);
+            return $userGroup->isAccepted();
+        }
+        return null !== $this->getUserGroupFromGroup($group);
+    }
+
+    /**
+     * @return ArrayCollection<Group>
+     */
+    public function getGroups()
+    {
+        $groups = new ArrayCollection();
+        foreach ($this->userGroups as $userGroup) {
+            $groups->add($userGroup->getGroup());
+        }
+        return $groups;
     }
 }