Added entities and properties for some future features.

This commit is contained in:
Jan Böhmer 2019-08-12 15:47:57 +02:00
parent bcdba8b3e0
commit 7826e3d2ad
49 changed files with 1477 additions and 362 deletions

View file

@ -0,0 +1,82 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Entity\UserSystem;
use App\Entity\Base\StructuralDBElement;
use App\Security\Interfaces\HasPermissionsInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* This entity represents an user group.
*
* @ORM\Entity()
* @ORM\Table("`groups`")
*/
class Group extends StructuralDBElement implements HasPermissionsInterface
{
/**
* @ORM\OneToMany(targetEntity="Group", mappedBy="parent")
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="group")
*/
protected $users;
/** @var PermissionsEmbed
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
*/
protected $permissions;
/**
* Returns the ID as an string, defined by the element class.
* This should have a form like P000014, for a part with ID 14.
*
* @return string The ID as a string;
*/
public function getIDString(): string
{
return 'G'.sprintf('%06d', $this->getID());
}
public function getPermissions(): PermissionsEmbed
{
return $this->permissions;
}
}

View file

@ -0,0 +1,397 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Entity\UserSystem;
use Doctrine\ORM\Mapping as ORM;
use Webmozart\Assert\Assert;
/**
* This entity represents the permission fields a user or group can have.
*
* @ORM\Embeddable()
*/
class PermissionsEmbed
{
/**
* Permission values.
*/
public const INHERIT = 0b00;
public const ALLOW = 0b01;
public const DISALLOW = 0b10;
/**
* Permission strings.
*/
public const STORELOCATIONS = 'storelocations';
public const FOOTRPINTS = 'footprints';
public const CATEGORIES = 'categories';
public const SUPPLIERS = 'suppliers';
public const MANUFACTURERS = 'manufacturers';
public const DEVICES = 'devices';
public const ATTACHMENT_TYPES = 'attachment_types';
public const TOOLS = 'tools';
public const PARTS = 'parts';
public const PARTS_NAME = 'parts_name';
public const PARTS_DESCRIPTION = 'parts_description';
public const PARTS_INSTOCK = 'parts_instock';
public const PARTS_MININSTOCK = 'parts_mininstock';
public const PARTS_FOOTPRINT = 'parts_footprint';
public const PARTS_COMMENT = 'parts_comment';
public const PARTS_STORELOCATION = 'parts_storelocation';
public const PARTS_MANUFACTURER = 'parts_manufacturer';
public const PARTS_ORDERDETAILS = 'parts_orderdetails';
public const PARTS_PRICES = 'parts_prices';
public const PARTS_ATTACHMENTS = 'parts_attachments';
public const PARTS_ORDER = 'parts_order';
public const GROUPS = 'groups';
public const USERS = 'users';
public const DATABASE = 'database';
public const CONFIG = 'config';
public const SYSTEM = 'system';
public const DEVICE_PARTS = 'devices_parts';
public const SELF = 'self';
public const LABELS = 'labels';
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $system = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $groups = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $users = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $self = 0;
/**
* @var int
* @ORM\Column(type="integer", name="system_config")
*/
protected $config = 0;
/**
* @var int
* @ORM\Column(type="integer", name="system_database")
*/
protected $database = 0;
/**
* @var int
* @ORM\Column(type="bigint")
*/
protected $parts = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_name = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_description = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_instock = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_mininstock = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_footprint = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_storelocation = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_manufacturer = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_comment = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_order = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_orderdetails = 0;
/**
* @var int
* @ORM\Column(type="smallint")
*/
protected $parts_prices = 0;
/**
* @var int
* @ORM\Column(type="smallint", name="parts_attachements")
*/
protected $parts_attachments = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $devices = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $devices_parts = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $storelocations = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $footprints = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $categories = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $suppliers = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $manufacturers = 0;
/**
* @var int
* @ORM\Column(type="integer", name="attachement_types")
*/
protected $attachment_types = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $tools = 0;
/**
* @var int
* @ORM\Column(type="integer")
*/
protected $labels = 0;
/**
* Checks whether a permission with the given name is valid for this object.
* @param string $permission_name The name of the permission which should be checked for.
* @return bool True if the permission is existing on this object.
*/
public function isValidPermissionName(string $permission_name) : bool
{
return isset($this->$permission_name);
}
/**
* Returns the bit pair value of the given permission.
*
* @param string $permission_name The name of the permission, for which the bit pair should be returned.
* @param int $bit_n The (lower) bit number of the bit pair, which should be read.
*
* @return int The value of the bit pair. Compare to the INHERIT, ALLOW, and DISALLOW consts in this class.
*/
public function getBitValue(string $permission_name, int $bit_n): int
{
if(!$this->isValidPermissionName($permission_name)) {
throw new \InvalidArgumentException('No permission with the given name is existing!');
}
$perm_int = $this->$permission_name;
return static::readBitPair($perm_int, $bit_n);
}
/**
* Returns the value of the operation for the given permission.
*
* @param string $permission_name The name of the permission, for which the operation should be returned.
* @param int $bit_n The (lower) bit number of the bit pair for the operation.
*
* @return bool|null The value of the operation. True, if the given operation is allowed, false if disallowed
* and null if it should inherit from parent.
*/
public function getPermissionValue(string $permission_name, int $bit_n): ?bool
{
$value = $this->getBitValue($permission_name, $bit_n);
if (self::ALLOW === $value) {
return true;
}
if (self::DISALLOW === $value) {
return false;
}
return null;
}
/**
* Sets the value of the given permission and operation.
* @param string $permission_name The name of the permission, for which the bit pair should be written.
* @param int $bit_n The (lower) bit number of the bit pair, which should be written.
* @param bool|null $new_value The new value for the operation:
* True, if the given operation is allowed, false if disallowed
* and null if it should inherit from parent.
* @return PermissionsEmbed The instance itself.
*/
public function setPermissionValue(string $permission_name, int $bit_n, ?bool $new_value) : self
{
//Determine which bit value the given value is.
if($new_value === true) {
$bit_value = static::ALLOW;
} elseif($new_value === false) {
$bit_value = static::DISALLOW;
} else {
$bit_value = static::INHERIT;
}
$this->setBitValue($permission_name, $bit_n, $bit_value);
return $this;
}
/**
* Sets the bit value of the given permission and operation.
* @param string $permission_name The name of the permission, for which the bit pair should be written.
* @param int $bit_n The (lower) bit number of the bit pair, which should be written.
* @param int $new_value The new (bit) value of the bit pair, which should be written.
* @return PermissionsEmbed The instance itself.
*/
public function setBitValue(string $permission_name, int $bit_n, int $new_value) : self
{
if(!$this->isValidPermissionName($permission_name)) {
throw new \InvalidArgumentException('No permission with the given name is existing!');
}
$this->$permission_name = static::writeBitPair($this->$permission_name, $bit_n, $new_value);
return $this;
}
/**
* Reads a bit pair from $data.
*
* @param $data int The data from where the bits should be extracted from.
* @param $n int The number of the lower bit (of the pair) that should be read. Starting from zero.
*
* @return int The value of the bit pair.
*/
final protected static function readBitPair(int $data, int $n): int
{
Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.');
if (0 !== $n % 2) {
throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!');
}
$mask = 0b11 << $n; //Create a mask for the data
return ($data & $mask) >> $n; //Apply mask and shift back
}
/**
* Writes a bit pair in the given $data and returns it.
*
* @param $data int The data which should be modified.
* @param $n int The number of the lower bit of the pair which should be written.
* @param $new int The new value of the pair.
*
* @return int The new data with the modified pair.
*/
final protected static function writeBitPair(int $data, int $n, int $new): int
{
Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.');
Assert::lessThanEq($new, 3, '$new must be smaller than 3, because a bit pair is written! Got %s.');
Assert::greaterThanEq($new, 0, '$new must not be negative, because a bit pair is written! Got %s.');
if (0 !== $n % 2) {
throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!');
}
$mask = 0b11 << $n; //Mask all bits that should be writen
$newval = $new << $n; //The new value.
$data = ($data & ~$mask) | ($newval & $mask);
return $data;
}
}

View file

@ -0,0 +1,471 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
declare(strict_types=1);
/**
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/.
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
namespace App\Entity\UserSystem;
use App\Entity\Base\NamedDBElement;
use App\Security\Interfaces\HasPermissionsInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This entity represents a user, which can log in and have permissions.
* Also this entity is able to save some informations about the user, like the names, email-address and other info.
*
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table("`users`")
*/
class User extends NamedDBElement implements UserInterface, HasPermissionsInterface
{
/** The User id of the anonymous user */
public const ID_ANONYMOUS = 1;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Assert\NotBlank
*/
protected $name = "";
/**
* //@ORM\Column(type="json").
*/
//protected $roles = [];
/**
* @var string|null The hashed password
* @ORM\Column(type="string", nullable=true)
*/
protected $password;
/**
* @var bool True if the user needs to change password after log in
* @ORM\Column(type="boolean")
*/
protected $need_pw_change = true;
/**
* @var string|null The first name of the User
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $first_name = '';
/**
* @var string|null The last name of the User
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $last_name = '';
/**
* @var string|null The department the user is working
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $department = '';
/**
* @var string|null The email address of the user
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email()
*/
protected $email = '';
/**
* @var string|null The language/locale the user prefers
* @ORM\Column(type="string", name="config_language", nullable=true)
*/
protected $language = '';
/**
* @var string|null The timezone the user prefers
* @ORM\Column(type="string", name="config_timezone", nullable=true)
*/
protected $timezone = '';
/**
* @var string|null The theme
* @ORM\Column(type="string", name="config_theme", nullable=true)
*/
protected $theme = '';
/**
* @var Group|null the group this user belongs to
* @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
*/
protected $group;
/** @var PermissionsEmbed
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
*/
protected $permissions;
/**
* @ORM\Column(type="string", name="config_currency")
*/
protected $currency;
/**
* @ORM\Column(type="text", name="config_image_path")
*/
protected $image_path;
/**
* @ORM\Column(type="text", name="config_instock_comment_w")
*/
protected $instock_comment_w;
/**
* @ORM\Column(type="text", name="config_instock_comment_a")
*/
protected $instock_comment_a;
/**
* Checks if the current user, is the user which represents the not logged in (anonymous) users.
*
* @return bool true if this user is the anonymous user
*/
public function isAnonymousUser(): bool
{
return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->name;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = [];
//$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
//$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
* Gets the password hash for this entity.
*/
public function getPassword(): string
{
return (string) $this->password;
}
/**
* Sets the password hash for this user.
*
* @param string $password
*
* @return User
*/
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* Returns the ID as an string, defined by the element class.
* This should have a form like P000014, for a part with ID 14.
*
* @return string The ID as a string;
*/
public function getIDString(): string
{
return 'U'.sprintf('%06d', $this->getID());
}
public function getPermissions(): PermissionsEmbed
{
return $this->permissions;
}
/************************************************
* Getters
************************************************/
/**
* Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
* Example: Max Muster (m.muster).
*
* @param bool $including_username include the username in the full name
*
* @return string a string with the full name of this user
*/
public function getFullName(bool $including_username = false): string
{
$str = $this->getFirstName().' '.$this->getLastName();
if ($including_username) {
$str .= ' ('.$this->getName().')';
}
return $str;
}
public function setName(string $new_name): NamedDBElement
{
// Anonymous user is not allowed to change its username
if (!$this->isAnonymousUser()) {
$this->name = $new_name;
}
return $this;
}
/**
* @return string
*/
public function getFirstName(): ?string
{
return $this->first_name;
}
/**
* @param string $first_name
*
* @return User
*/
public function setFirstName(?string $first_name): User
{
$this->first_name = $first_name;
return $this;
}
/**
* @return string
*/
public function getLastName(): ?string
{
return $this->last_name;
}
/**
* @param string $last_name
*
* @return User
*/
public function setLastName(?string $last_name): User
{
$this->last_name = $last_name;
return $this;
}
/**
* @return string
*/
public function getDepartment(): ?string
{
return $this->department;
}
/**
* @param string $department
*
* @return User
*/
public function setDepartment(?string $department): User
{
$this->department = $department;
return $this;
}
/**
* @return string
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param string $email
*
* @return User
*/
public function setEmail(?string $email): User
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getLanguage(): ?string
{
return $this->language;
}
/**
* @param string $language
*
* @return User
*/
public function setLanguage(?string $language): User
{
$this->language = $language;
return $this;
}
/**
* @return string
*/
public function getTimezone(): ?string
{
return $this->timezone;
}
/**
* @param string $timezone
*
* @return User
*/
public function setTimezone(?string $timezone): User
{
$this->timezone = $timezone;
return $this;
}
/**
* @return string
*/
public function getTheme(): ?string
{
return $this->theme;
}
/**
* @param string $theme
*
* @return User
*/
public function setTheme(?string $theme): User
{
$this->theme = $theme;
return $this;
}
public function getGroup(): ?Group
{
return $this->group;
}
public function setGroup(?Group $group): self
{
$this->group = $group;
return $this;
}
public function __toString()
{
return $this->getFullName(true);
}
}