added team permissions (#996)

This commit is contained in:
Kevin Papst
2019-08-16 01:22:33 +02:00
committed by GitHub
parent 9611646bc6
commit 561ec3b1e9
124 changed files with 4122 additions and 362 deletions

View File

@@ -160,9 +160,26 @@ class Customer implements EntityWithMetaFields
*/
private $meta;
/**
* @var Team[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Team", cascade={"remove", "persist"}, inversedBy="customers")
* @ORM\JoinTable(
* name="kimai2_customers_teams",
* joinColumns={
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $teams;
public function __construct()
{
$this->meta = new ArrayCollection();
$this->teams = new ArrayCollection();
}
public function getId(): ?int
@@ -399,6 +416,33 @@ class Customer implements EntityWithMetaFields
return $this;
}
public function addTeam(Team $team)
{
if ($this->teams->contains($team)) {
return $this;
}
$this->teams->add($team);
$team->addCustomer($this);
}
public function removeTeam(Team $team)
{
if (!$this->teams->contains($team)) {
return;
}
$this->teams->removeElement($team);
$team->removeCustomer($this);
}
/**
* @return Collection<Team>
*/
public function getTeams(): Collection
{
return $this->teams;
}
/**
* @return string
*/

View File

@@ -90,9 +90,26 @@ class Project implements EntityWithMetaFields
*/
private $meta;
/**
* @var Team[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Team", cascade={"remove", "persist"}, inversedBy="projects")
* @ORM\JoinTable(
* name="kimai2_projects_teams",
* joinColumns={
* @ORM\JoinColumn(name="project_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $teams;
public function __construct()
{
$this->meta = new ArrayCollection();
$this->teams = new ArrayCollection();
}
public function getId(): ?int
@@ -223,6 +240,33 @@ class Project implements EntityWithMetaFields
return $this;
}
public function addTeam(Team $team)
{
if ($this->teams->contains($team)) {
return;
}
$this->teams->add($team);
$team->addProject($this);
}
public function removeTeam(Team $team)
{
if (!$this->teams->contains($team)) {
return;
}
$this->teams->removeElement($team);
$team->removeProject($this);
}
/**
* @return Collection<Team>
*/
public function getTeams(): Collection
{
return $this->teams;
}
/**
* @return string
*/

210
src/Entity/Team.php Normal file
View File

@@ -0,0 +1,210 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="kimai2_teams",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"name"})
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\TeamRepository")
* @UniqueEntity("name")
*/
class Team
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100, nullable=false)
* @Assert\NotBlank()
* @Assert\Length(min=2, max=100)
*/
private $name;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
* @Assert\NotNull()
*/
private $teamlead;
/**
* @var User[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="User", mappedBy="teams", fetch="EXTRA_LAZY")
*/
private $users;
/**
* @var Customer[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Customer", mappedBy="teams", fetch="EXTRA_LAZY")
*/
private $customers;
/**
* @var Project[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Project", mappedBy="teams", fetch="EXTRA_LAZY")
*/
private $projects;
public function __construct()
{
$this->users = new ArrayCollection();
$this->customers = new ArrayCollection();
$this->projects = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setName(string $name): Team
{
$this->name = $name;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function getTeamLead(): ?User
{
return $this->teamlead;
}
public function isTeamlead(User $user): bool
{
return $this->teamlead === $user;
}
public function setTeamLead(User $teamlead): Team
{
$this->teamlead = $teamlead;
$this->addUser($teamlead);
return $this;
}
public function hasUser(User $user): bool
{
return $this->users->contains($user);
}
public function addUser(User $user)
{
if ($this->users->contains($user)) {
return;
}
$this->users->add($user);
$user->addTeam($this);
}
public function removeUser(User $user)
{
if (!$this->users->contains($user)) {
return;
}
$this->users->removeElement($user);
$user->removeTeam($this);
}
/**
* @return Collection<User>
*/
public function getUsers(): iterable
{
return $this->users;
}
public function addCustomer(Customer $customer)
{
if ($this->customers->contains($customer)) {
return;
}
$this->customers->add($customer);
$customer->addTeam($this);
}
public function removeCustomer(Customer $customer)
{
if (!$this->customers->contains($customer)) {
return;
}
$this->customers->removeElement($customer);
$customer->removeTeam($this);
}
/**
* @return Collection<Customer>
*/
public function getCustomers(): iterable
{
return $this->customers;
}
public function addProject(Project $project)
{
if ($this->projects->contains($project)) {
return;
}
$this->projects->add($project);
$project->addTeam($this);
}
public function removeProject(Project $project)
{
if (!$this->projects->contains($project)) {
return;
}
$this->projects->removeElement($project);
$project->removeTeam($this);
}
/**
* @return Collection<Project>
*/
public function getProjects(): iterable
{
return $this->projects;
}
/**
* @return string
*/
public function __toString()
{
return $this->getName();
}
}

View File

@@ -140,14 +140,14 @@ class Timesheet implements EntityWithMetaFields
/**
* @var Tag[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="timesheets", cascade={"persist"})
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="timesheets", cascade={"remove", "persist"})
* @ORM\JoinTable(
* name="kimai2_timesheet_tags",
* joinColumns={
* @ORM\JoinColumn(name="timesheet_id", referencedColumnName="id")
* @ORM\JoinColumn(name="timesheet_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/

View File

@@ -95,6 +95,22 @@ class User extends BaseUser implements UserInterface
*/
private $preferences;
/**
* @var Team[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Team", inversedBy="users", cascade={"remove", "persist"})
* @ORM\JoinTable(
* name="kimai2_users_teams",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $teams;
/**
* User constructor.
*/
@@ -103,6 +119,7 @@ class User extends BaseUser implements UserInterface
parent::__construct();
$this->registeredAt = new \DateTime();
$this->preferences = new ArrayCollection();
$this->teams = new ArrayCollection();
}
public function getId(): ?int
@@ -274,6 +291,55 @@ class User extends BaseUser implements UserInterface
return $this;
}
public function addTeam(Team $team): User
{
if ($this->teams->contains($team)) {
return $this;
}
$this->teams->add($team);
$team->addUser($this);
return $this;
}
public function removeTeam(Team $team)
{
if (!$this->teams->contains($team)) {
return;
}
$this->teams->removeElement($team);
$team->removeUser($this);
}
/**
* @return Collection<Team>
*/
public function getTeams(): Collection
{
return $this->teams;
}
public function isInTeam(Team $team): bool
{
return $this->teams->contains($team);
}
public function isTeamleadOf(Team $team): bool
{
return $team->getTeamLead() === $this;
}
public function isTeamlead(): bool
{
return $this->hasRole(static::ROLE_TEAMLEAD);
}
public function isAdmin(): bool
{
return $this->hasRole(static::ROLE_ADMIN);
}
/**
* @return string
*/

View File

@@ -38,7 +38,6 @@ class UserPreference
* @ORM\Column(name="id", type="integer")
*/
private $id;
/**
* @var User
*
@@ -47,7 +46,6 @@ class UserPreference
* @Assert\NotNull()
*/
private $user;
/**
* @var string
*
@@ -55,28 +53,29 @@ class UserPreference
* @Assert\Length(min=2, max=50)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="value", type="string", length=255, nullable=true)
*/
private $value;
/**
* @var string
*/
protected $type;
private $type;
/**
* @var bool
*/
protected $enabled = true;
private $enabled = true;
/**
* @var Constraint[]
*/
protected $constraints = [];
private $constraints = [];
/**
* An array of options for the form element
* @var array
*/
private $options = [];
/**
* @return int
@@ -237,4 +236,27 @@ class UserPreference
{
return $this->constraints;
}
/**
* Set an array of options for the FormType.
*
* @param array $options
* @return UserPreference
*/
public function setOptions(array $options): UserPreference
{
$this->options = $options;
return $this;
}
/**
* Returns an array with options for the FormType.
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
}