aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/GroupBundle/Entity/Group.php
blob: 31c828377aabd073ca386f88cd00c492b2608501 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php

namespace Wallabag\GroupBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Model\Group as BaseGroup;
use Doctrine\ORM\Mapping as ORM;
use Wallabag\UserBundle\Entity\User;

/**
 * @ORM\Entity(repositoryClass="Wallabag\GroupBundle\Repository\GroupRepository")
 * @ORM\Table(name="`group`")
 */
class Group extends BaseGroup
{

    /**
     * User Roles
     */

    /** User can only preview presentations */
    const ROLE_READ_ONLY = 1;

    /** User can create new presentations */
    const ROLE_WRITE = 2;

    /** User can manage all group presentations */
    const ROLE_MANAGE_ENTRIES = 3;

    /** User can manage users in the group */
    const ROLE_MANAGE_USERS = 5;

    /** User can rename and delete the group */
    const ROLE_ADMIN = 10;

    /**
     * Group join access
     */

    /** Any user can join the group */
    const ACCESS_OPEN = 1;

    /** An user needs to request to join the group */
    const ACCESS_REQUEST = 2;

    /** An user need the password to access the group */
    const ACCESS_PASSWORD = 3;

    /** An user needs to be invited to join the group */
    const ACCESS_INVITATION_ONLY = 4;

    /** An user needs to be invited to join the group, and the group is not publicly listed */
    const ACCESS_HIDDEN = 10;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer", options={"default" : 1})
     */
    protected $acceptSystem;

    /**
     * @ORM\Column(type="integer", options={"default" : 2})
     */
    protected $defaultRole;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $password;
    protected $plainPassword;

    /**
     * @ORM\ManyToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="groupShares", cascade={"persist"})
     */
    protected $entries;

    /**
     * @ORM\OneToMany(targetEntity="UserGroup", mappedBy="group", cascade={"persist"})
     */
    protected $users;

    public function __construct($name = '', array $roles = [])
    {
        parent::__construct($name, $roles);
        $this->defaultRole = self::ROLE_READ_ONLY;
        $this->acceptSystem = self::ACCESS_REQUEST;
    }

    /**
     * @return ArrayCollection
     */
    public function getUsers()
    {
        $userObj = new ArrayCollection();
        foreach ($this->users as $userGroup) {
            /** @var UserGroup $userGroup */
            $userObj->add($userGroup->getUser());
        }
        return $userObj;
    }

    /**
     * @return int
     */
    public function getDefaultRole()
    {
        return $this->defaultRole;
    }

    /**
     * @return int
     */
    public function getAcceptSystem()
    {
        return $this->acceptSystem;
    }

    /**
     * @param int $acceptSystem
     */
    public function setAcceptSystem($acceptSystem)
    {
        $this->acceptSystem = $acceptSystem;
    }

    /**
     * @return string
     */
    public function getPassword()
    {
        return $this->password ?: '';
    }

    /**
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * @return string
     */
    public function getPlainPassword()
    {
        return $this->plainPassword ?: '';
    }

    /**
     * @param string $plainPassword
     */
    public function setPlainPassword($plainPassword)
    {
        $this->plainPassword = $plainPassword;
    }

    /**
     * @param int $defaultRole
     */
    public function setDefaultRole($defaultRole)
    {
        $this->defaultRole = $defaultRole;
    }

    public function __toString()
    {
        return $this->name;
    }

    public function getRequests()
    {
        $requests = new ArrayCollection();
        foreach ($this->users as $user) /** @var UserGroup $user */
        {
            if (!$user->isAccepted()) {
                $requests->add($user->getUser());
            }
        }
        return $requests;
    }

    public function getInvited()
    {
        $invited = new ArrayCollection();
        foreach ($this->users as $userGroup) /** @var UserGroup $userGroup */
        {
            if ($userGroup->getInvitation()) {
                $invited->add($userGroup);
            }
        }
        return $invited;
    }
}