diff options
Diffstat (limited to 'src/Wallabag/GroupBundle/Entity')
-rw-r--r-- | src/Wallabag/GroupBundle/Entity/Group.php | 172 | ||||
-rw-r--r-- | src/Wallabag/GroupBundle/Entity/Invitation.php | 73 | ||||
-rw-r--r-- | src/Wallabag/GroupBundle/Entity/UserGroup.php | 126 |
3 files changed, 356 insertions, 15 deletions
diff --git a/src/Wallabag/GroupBundle/Entity/Group.php b/src/Wallabag/GroupBundle/Entity/Group.php index 1381d1ea..31c82837 100644 --- a/src/Wallabag/GroupBundle/Entity/Group.php +++ b/src/Wallabag/GroupBundle/Entity/Group.php | |||
@@ -8,11 +8,50 @@ use Doctrine\ORM\Mapping as ORM; | |||
8 | use Wallabag\UserBundle\Entity\User; | 8 | use Wallabag\UserBundle\Entity\User; |
9 | 9 | ||
10 | /** | 10 | /** |
11 | * @ORM\Entity | 11 | * @ORM\Entity(repositoryClass="Wallabag\GroupBundle\Repository\GroupRepository") |
12 | * @ORM\Table(name="`group`") | 12 | * @ORM\Table(name="`group`") |
13 | */ | 13 | */ |
14 | class Group extends BaseGroup | 14 | class Group extends BaseGroup |
15 | { | 15 | { |
16 | |||
17 | /** | ||
18 | * User Roles | ||
19 | */ | ||
20 | |||
21 | /** User can only preview presentations */ | ||
22 | const ROLE_READ_ONLY = 1; | ||
23 | |||
24 | /** User can create new presentations */ | ||
25 | const ROLE_WRITE = 2; | ||
26 | |||
27 | /** User can manage all group presentations */ | ||
28 | const ROLE_MANAGE_ENTRIES = 3; | ||
29 | |||
30 | /** User can manage users in the group */ | ||
31 | const ROLE_MANAGE_USERS = 5; | ||
32 | |||
33 | /** User can rename and delete the group */ | ||
34 | const ROLE_ADMIN = 10; | ||
35 | |||
36 | /** | ||
37 | * Group join access | ||
38 | */ | ||
39 | |||
40 | /** Any user can join the group */ | ||
41 | const ACCESS_OPEN = 1; | ||
42 | |||
43 | /** An user needs to request to join the group */ | ||
44 | const ACCESS_REQUEST = 2; | ||
45 | |||
46 | /** An user need the password to access the group */ | ||
47 | const ACCESS_PASSWORD = 3; | ||
48 | |||
49 | /** An user needs to be invited to join the group */ | ||
50 | const ACCESS_INVITATION_ONLY = 4; | ||
51 | |||
52 | /** An user needs to be invited to join the group, and the group is not publicly listed */ | ||
53 | const ACCESS_HIDDEN = 10; | ||
54 | |||
16 | /** | 55 | /** |
17 | * @ORM\Id | 56 | * @ORM\Id |
18 | * @ORM\Column(type="integer") | 57 | * @ORM\Column(type="integer") |
@@ -21,38 +60,141 @@ class Group extends BaseGroup | |||
21 | protected $id; | 60 | protected $id; |
22 | 61 | ||
23 | /** | 62 | /** |
24 | * @ORM\ManyToMany(targetEntity="Wallabag\UserBundle\Entity\User", mappedBy="groups", cascade={"persist"}) | 63 | * @ORM\Column(type="integer", options={"default" : 1}) |
64 | */ | ||
65 | protected $acceptSystem; | ||
66 | |||
67 | /** | ||
68 | * @ORM\Column(type="integer", options={"default" : 2}) | ||
69 | */ | ||
70 | protected $defaultRole; | ||
71 | |||
72 | /** | ||
73 | * @ORM\Column(type="string", nullable=true) | ||
74 | */ | ||
75 | protected $password; | ||
76 | protected $plainPassword; | ||
77 | |||
78 | /** | ||
79 | * @ORM\ManyToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="groupShares", cascade={"persist"}) | ||
80 | */ | ||
81 | protected $entries; | ||
82 | |||
83 | /** | ||
84 | * @ORM\OneToMany(targetEntity="UserGroup", mappedBy="group", cascade={"persist"}) | ||
25 | */ | 85 | */ |
26 | protected $users; | 86 | protected $users; |
27 | 87 | ||
28 | public function getUsers() | 88 | public function __construct($name = '', array $roles = []) |
29 | { | 89 | { |
30 | return $this->users ?: $this->users = new ArrayCollection(); | 90 | parent::__construct($name, $roles); |
91 | $this->defaultRole = self::ROLE_READ_ONLY; | ||
92 | $this->acceptSystem = self::ACCESS_REQUEST; | ||
31 | } | 93 | } |
32 | 94 | ||
33 | public function addUser(User $user) | 95 | /** |
96 | * @return ArrayCollection | ||
97 | */ | ||
98 | public function getUsers() | ||
34 | { | 99 | { |
35 | if (!$this->getUsers()->contains($user)) { | 100 | $userObj = new ArrayCollection(); |
36 | $this->getUsers()->add($user); | 101 | foreach ($this->users as $userGroup) { |
102 | /** @var UserGroup $userGroup */ | ||
103 | $userObj->add($userGroup->getUser()); | ||
37 | } | 104 | } |
105 | return $userObj; | ||
106 | } | ||
38 | 107 | ||
39 | return $this; | 108 | /** |
109 | * @return int | ||
110 | */ | ||
111 | public function getDefaultRole() | ||
112 | { | ||
113 | return $this->defaultRole; | ||
40 | } | 114 | } |
41 | 115 | ||
42 | /** | 116 | /** |
43 | * {@inheritdoc} | 117 | * @return int |
44 | */ | 118 | */ |
45 | public function removeUser(User $user) | 119 | public function getAcceptSystem() |
46 | { | 120 | { |
47 | if ($this->getUsers()->contains($user)) { | 121 | return $this->acceptSystem; |
48 | $this->getUsers()->removeElement($user); | 122 | } |
49 | } | 123 | |
124 | /** | ||
125 | * @param int $acceptSystem | ||
126 | */ | ||
127 | public function setAcceptSystem($acceptSystem) | ||
128 | { | ||
129 | $this->acceptSystem = $acceptSystem; | ||
130 | } | ||
50 | 131 | ||
51 | return $this; | 132 | /** |
133 | * @return string | ||
134 | */ | ||
135 | public function getPassword() | ||
136 | { | ||
137 | return $this->password ?: ''; | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * @param string $password | ||
142 | */ | ||
143 | public function setPassword($password) | ||
144 | { | ||
145 | $this->password = $password; | ||
146 | } | ||
147 | |||
148 | /** | ||
149 | * @return string | ||
150 | */ | ||
151 | public function getPlainPassword() | ||
152 | { | ||
153 | return $this->plainPassword ?: ''; | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * @param string $plainPassword | ||
158 | */ | ||
159 | public function setPlainPassword($plainPassword) | ||
160 | { | ||
161 | $this->plainPassword = $plainPassword; | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * @param int $defaultRole | ||
166 | */ | ||
167 | public function setDefaultRole($defaultRole) | ||
168 | { | ||
169 | $this->defaultRole = $defaultRole; | ||
52 | } | 170 | } |
53 | 171 | ||
54 | public function __toString() | 172 | public function __toString() |
55 | { | 173 | { |
56 | return $this->getName(); | 174 | return $this->name; |
175 | } | ||
176 | |||
177 | public function getRequests() | ||
178 | { | ||
179 | $requests = new ArrayCollection(); | ||
180 | foreach ($this->users as $user) /** @var UserGroup $user */ | ||
181 | { | ||
182 | if (!$user->isAccepted()) { | ||
183 | $requests->add($user->getUser()); | ||
184 | } | ||
185 | } | ||
186 | return $requests; | ||
187 | } | ||
188 | |||
189 | public function getInvited() | ||
190 | { | ||
191 | $invited = new ArrayCollection(); | ||
192 | foreach ($this->users as $userGroup) /** @var UserGroup $userGroup */ | ||
193 | { | ||
194 | if ($userGroup->getInvitation()) { | ||
195 | $invited->add($userGroup); | ||
196 | } | ||
197 | } | ||
198 | return $invited; | ||
57 | } | 199 | } |
58 | } | 200 | } |
diff --git a/src/Wallabag/GroupBundle/Entity/Invitation.php b/src/Wallabag/GroupBundle/Entity/Invitation.php new file mode 100644 index 00000000..6946bede --- /dev/null +++ b/src/Wallabag/GroupBundle/Entity/Invitation.php | |||
@@ -0,0 +1,73 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\GroupBundle\Entity; | ||
4 | |||
5 | use Doctrine\ORM\Mapping as ORM; | ||
6 | |||
7 | /** | ||
8 | * @ORM\Entity | ||
9 | */ | ||
10 | class Invitation | ||
11 | { | ||
12 | /** | ||
13 | * @ORM\Id | ||
14 | * @ORM\Column(type="string", length=6) | ||
15 | */ | ||
16 | protected $code; | ||
17 | |||
18 | /** | ||
19 | * @var \DateTime | ||
20 | * @ORM\Column(type="datetime") | ||
21 | */ | ||
22 | protected $date; | ||
23 | |||
24 | /** | ||
25 | * @ORM\OneToOne(targetEntity="UserGroup", mappedBy="invitation") | ||
26 | */ | ||
27 | protected $userGroup; | ||
28 | |||
29 | public function __construct(UserGroup $userGroup) | ||
30 | { | ||
31 | // generate identifier only once, here a 6 characters length code | ||
32 | $this->code = substr(md5(uniqid(rand(), true)), 0, 6); | ||
33 | $this->date = new \DateTime(); | ||
34 | $this->userGroup = $userGroup; | ||
35 | } | ||
36 | |||
37 | public function getCode() | ||
38 | { | ||
39 | return $this->code; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * @return \DateTime | ||
44 | */ | ||
45 | public function getDate(): \DateTime | ||
46 | { | ||
47 | return $this->date; | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * @param \DateTime $date | ||
52 | */ | ||
53 | public function setDate(\DateTime $date) | ||
54 | { | ||
55 | $this->date = $date; | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * @return mixed | ||
60 | */ | ||
61 | public function getUserGroup(): UserGroup | ||
62 | { | ||
63 | return $this->userGroup; | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * @param mixed $userGroup | ||
68 | */ | ||
69 | public function setUserGroup(UserGroup $userGroup) | ||
70 | { | ||
71 | $this->userGroup = $userGroup; | ||
72 | } | ||
73 | } | ||
diff --git a/src/Wallabag/GroupBundle/Entity/UserGroup.php b/src/Wallabag/GroupBundle/Entity/UserGroup.php new file mode 100644 index 00000000..22d1400a --- /dev/null +++ b/src/Wallabag/GroupBundle/Entity/UserGroup.php | |||
@@ -0,0 +1,126 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\GroupBundle\Entity; | ||
4 | |||
5 | use Doctrine\ORM\Mapping as ORM; | ||
6 | use FOS\UserBundle\Model\GroupInterface; | ||
7 | use Symfony\Component\Validator\Constraints as Assert; | ||
8 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
9 | use Wallabag\UserBundle\Entity\User; | ||
10 | |||
11 | /** | ||
12 | * @ORM\Entity(repositoryClass="Wallabag\GroupBundle\Repository\UserGroupRepository") | ||
13 | * @UniqueEntity({"user_id", "group_id"}) | ||
14 | * @ORM\Table(name="fos_user_group") | ||
15 | */ | ||
16 | class UserGroup | ||
17 | { | ||
18 | /** | ||
19 | * @ORM\Column(name="id", type="integer") | ||
20 | * @ORM\Id | ||
21 | * @ORM\GeneratedValue(strategy="AUTO") | ||
22 | */ | ||
23 | private $id; | ||
24 | |||
25 | /** | ||
26 | * @ORM\Column(name="role", type="integer") | ||
27 | */ | ||
28 | private $role; | ||
29 | |||
30 | /** | ||
31 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="userGroups") | ||
32 | * @ORM\JoinColumn(name="user_id", referencedColumnName="id") | ||
33 | */ | ||
34 | private $user; | ||
35 | |||
36 | /** | ||
37 | * @ORM\ManyToOne(targetEntity="Wallabag\GroupBundle\Entity\Group", inversedBy="users") | ||
38 | * @ORM\JoinColumn(name="group_id", referencedColumnName="id") | ||
39 | */ | ||
40 | private $group; | ||
41 | |||
42 | /** | ||
43 | * @ORM\Column(name="accepted", type="boolean", options={"default" : false}) | ||
44 | */ | ||
45 | private $accepted; | ||
46 | |||
47 | /** | ||
48 | * @ORM\OneToOne(targetEntity="Wallabag\GroupBundle\Entity\Invitation", inversedBy="userGroup", cascade={"persist", "remove"}) | ||
49 | * @ORM\JoinColumn(name="invitation", referencedColumnName="code") | ||
50 | */ | ||
51 | protected $invitation; | ||
52 | |||
53 | /** | ||
54 | * UserGroup constructor. | ||
55 | * @param User $user | ||
56 | * @param Group $group | ||
57 | * @param $role | ||
58 | */ | ||
59 | public function __construct(User $user, Group $group, $role, $request = false) | ||
60 | { | ||
61 | $this->user = $user; | ||
62 | $this->group = $group; | ||
63 | $this->role = $role; | ||
64 | $this->accepted = $request; | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * @return Group | ||
69 | */ | ||
70 | public function getGroup() | ||
71 | { | ||
72 | return $this->group; | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * @return int | ||
77 | */ | ||
78 | public function getRole() | ||
79 | { | ||
80 | return $this->role; | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * @return User | ||
85 | */ | ||
86 | public function getUser() | ||
87 | { | ||
88 | return $this->user; | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * @param int $role | ||
93 | * @return UserGroup | ||
94 | */ | ||
95 | public function setRole($role) | ||
96 | { | ||
97 | $this->role = $role; | ||
98 | return $this; | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * @param bool $accepted | ||
103 | */ | ||
104 | public function setAccepted($accepted) | ||
105 | { | ||
106 | $this->accepted = $accepted; | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * @return bool | ||
111 | */ | ||
112 | public function isAccepted() | ||
113 | { | ||
114 | return $this->accepted; | ||
115 | } | ||
116 | |||
117 | public function setInvitation($invitation) | ||
118 | { | ||
119 | $this->invitation = $invitation; | ||
120 | } | ||
121 | |||
122 | public function getInvitation() | ||
123 | { | ||
124 | return $this->invitation; | ||
125 | } | ||
126 | } | ||