Last active
May 13, 2018 22:09
-
-
Save ADCPD/6f8bc06c874f80de14234d720c373f49 to your computer and use it in GitHub Desktop.
Working with PostgreSQL & Symfony2.8 (Working Model with basic configuration)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
imports: | |
- { resource: parameters.yml } | |
- { resource: security.yml } | |
- { resource: services.yml } | |
#- { resource: extensions/migration.yml } | |
# Put parameters here that don't need to change on each machine where the app is deployed | |
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration | |
parameters: | |
locale: fr | |
framework: | |
#esi: ~ | |
#translator: { fallbacks: ['%locale%'] } | |
secret: '%secret%' | |
router: | |
resource: '%kernel.root_dir%/config/routing.yml' | |
strict_requirements: ~ | |
form: ~ | |
csrf_protection: ~ | |
validation: { enable_annotations: true } | |
serializer: { enable_annotations: true } | |
templating: | |
engines: ['twig'] | |
default_locale: '%locale%' | |
trusted_hosts: ~ | |
trusted_proxies: ~ | |
session: | |
# handler_id set to null will use default session handler from php.ini | |
handler_id: ~ | |
fragments: ~ | |
http_method_override: true | |
# Twig Configuration | |
twig: | |
debug: '%kernel.debug%' | |
strict_variables: '%kernel.debug%' | |
# Doctrine Configuration | |
doctrine: | |
dbal: | |
# driver: pdo_pgsql | |
driver: "%database_driver%" | |
host: 127.0.0.1 | |
port: 5432 | |
dbname: "%database_name%" | |
user: "%database_user%" | |
password: "%database_password%" | |
charset: UTF8 | |
# if using pdo_sqlite as your database driver: | |
# 1. add the path in parameters.yml | |
# e.g. database_path: '%kernel.root_dir%/data/data.db3' | |
# 2. Uncomment database_path in parameters.yml.dist | |
# 3. Uncomment next line: | |
#path: '%database_path%' | |
orm: | |
auto_generate_proxy_classes: '%kernel.debug%' | |
naming_strategy: doctrine.orm.naming_strategy.underscore | |
auto_mapping: true | |
# Swiftmailer Configuration | |
swiftmailer: | |
transport: '%mailer_transport%' | |
host: '%mailer_host%' | |
username: '%mailer_user%' | |
password: '%mailer_password%' | |
spool: { type: memory } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* create SCHEMA of database */ | |
CREATE SCHEMA agathe | |
AUTHORIZATION postgres; | |
/** Create User for the SCHEMA agathe **/ | |
CREATE USER postgres WITH | |
LOGIN | |
SUPERUSER | |
INHERIT | |
CREATEDB | |
CREATEROLE | |
REPLICATION; | |
/* create Entity utilisateurs */ | |
CREATE TABLE agathe.utilisateurs | |
( | |
tech_id integer NOT NULL, | |
nom character varying(55) COLLATE pg_catalog."default" DEFAULT NULL::character varying, | |
prenom character varying(55) COLLATE pg_catalog."default" DEFAULT NULL::character varying, | |
email character varying(255) COLLATE pg_catalog."default" DEFAULT NULL::character varying, | |
portable character varying(12) COLLATE pg_catalog."default" DEFAULT NULL::character varying, | |
fix character varying(12) COLLATE pg_catalog."default" DEFAULT NULL::character varying, | |
matricule character varying(12) COLLATE pg_catalog."default" NOT NULL, | |
roles json NOT NULL, | |
locked boolean NOT NULL, | |
expired boolean NOT NULL, | |
enabled boolean NOT NULL, | |
create_dt timestamp(0) without time zone DEFAULT NULL::timestamp without time zone, | |
update_dt timestamp(0) without time zone DEFAULT NULL::timestamp without time zone, | |
CONSTRAINT utilisateurs_pkey PRIMARY KEY (tech_id) | |
) | |
WITH ( | |
OIDS = FALSE | |
) | |
TABLESPACE pg_default; | |
ALTER TABLE agathe.utilisateurs | |
OWNER to postgres; | |
/* create Sequence utilisateurs to generate ID (tech_id) */ | |
CREATE SEQUENCE agathe.utilisateurs_id_seq | |
INCREMENT 999999 | |
START 1 | |
MINVALUE 1 | |
MAXVALUE 9223372036854775807 | |
CACHE 1; | |
ALTER SEQUENCE agathe.utilisateurs_id_seq | |
OWNER TO postgres; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
database_driver: pdo_pgsql | |
database_host: localhost | |
database_port: 5432 | |
database_name: agathe | |
database_user: postgres | |
database_password: root | |
mailer_transport: smtp | |
mailer_host: 127.0.0.1 | |
mailer_user: null | |
mailer_password: null | |
secret: ThisTokenIsNotSoSecretChangeIt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AuthentificationBundle\Entity; | |
use AppBundle\Communs\Consts\StaticConst; | |
use AppBundle\Communs\Traits\DateTimeTools; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Class utilisateurs | |
* @package AuthentificationBundle\Entity | |
* @ORM\Entity | |
* @ORM\Table(name="agathe.utilisateurs") | |
* | |
// * @ORM\Entity(repositoryClass="Administration\UserBundle\Repository\UserRepository") | |
*/ | |
class Utilisateurs | |
{ | |
/** | |
*@var int | |
* | |
* @ORM\Column(name="tech_id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="SEQUENCE") | |
* @ORM\SequenceGenerator(sequenceName="agathe.examens_id_seq", initialValue=1, allocationSize=999999) | |
*/ | |
private $techid; | |
/** | |
* @var string | |
* @ORM\Column(name="nom", type="string", length=55, nullable=true) | |
*/ | |
protected $nom; | |
/** | |
* @var string | |
* @ORM\Column(name="prenom", type="string", length=55, nullable=true) | |
*/ | |
protected $prenom; | |
/** | |
* @var string | |
* @ORM\Column(name="email", type="string", length=255, nullable=true) | |
*/ | |
protected $email; | |
/** | |
* @var string | |
* @ORM\Column(name="portable", type="string", length=12, nullable=true) | |
*/ | |
protected $portable; | |
/** | |
* @var string | |
* @ORM\Column(name="fix", type="string", length=12, nullable=true) | |
*/ | |
protected $fix; | |
/** | |
* @var string | |
* @ORM\Column(name="matricule", type="string", length=12, nullable=false) | |
*/ | |
protected $matricule; | |
/** | |
* | |
* @ORM\Column(name="roles", type="json_array", length=12, nullable=false) | |
*/ | |
protected $roles; | |
/*** | |
* @var string | |
* @ORM\Column(name="password", type="string", length=12, nullable=false) | |
*/ | |
protected $password; | |
/** | |
* Set techid | |
* | |
* @param string $techid | |
* | |
* @return Examens | |
*/ | |
public function setTechid($techid) | |
{ | |
$this->techid = $techid; | |
return $this; | |
} | |
/** | |
* Get techid | |
* | |
* @return string | |
*/ | |
public function getTechid() | |
{ | |
return $this->techid; | |
} | |
/** | |
* @@var boolean | |
* @ORM\Column(name="locked", type="boolean") | |
*/ | |
protected $locked; | |
/** | |
* @@var boolean | |
* @ORM\Column(name="expired", type="boolean") | |
*/ | |
protected $expired; | |
/** | |
* @var \DateTime | |
*/ | |
protected $expiresAt; | |
/** | |
* @@var boolean | |
* @ORM\Column(name="enabled", type="boolean") | |
*/ | |
protected $enabled; | |
/** | |
* | |
* @var string | |
*/ | |
protected $salt; | |
/** | |
* Utilisateurs constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36); | |
$this->enabled = false; | |
$this->locked = false; | |
$this->expired = false; | |
$this->roles = array(); | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getId() | |
{ | |
return $this->getTechid(); | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getNom() | |
{ | |
return $this->nom; | |
} | |
/** | |
* @param mixed $nom | |
*/ | |
public function setNom($nom) | |
{ | |
$this->nom = $nom; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getPrenom() | |
{ | |
return $this->prenom; | |
} | |
/** | |
* @param mixed $prenom | |
*/ | |
public function setPrenom($prenom) | |
{ | |
$this->prenom = $prenom; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
/** | |
* @param mixed $email | |
*/ | |
public function setEmail($email) | |
{ | |
$this->email = $email; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getPortable() | |
{ | |
return $this->portable; | |
} | |
/** | |
* @param mixed $portable | |
*/ | |
public function setPortable($portable) | |
{ | |
$this->portable = $portable; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getFix() | |
{ | |
return $this->fix; | |
} | |
/** | |
* @param mixed $fix | |
*/ | |
public function setFix($fix) | |
{ | |
$this->fix = $fix; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getMatricule() | |
{ | |
return $this->matricule; | |
} | |
/** | |
* @param mixed $matricule | |
*/ | |
public function setMatricule($matricule) | |
{ | |
$this->matricule = $matricule; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getPassword() | |
{ | |
return $this->password; | |
} | |
/** | |
* @param mixed $password | |
*/ | |
public function setPassword($password) | |
{ | |
$this->password = $password; | |
} | |
use DateTimeTools; | |
public function addRoles($role) | |
{ | |
$role = strtoupper($role); | |
if ($role === StaticConst::ROLE_DEFAULT) { | |
return $this; | |
} | |
if (!in_array($role, $this->roles, true)) { | |
$this->roles[] = $role; | |
} | |
return $this; | |
} | |
public function getSalt() | |
{ | |
return $this->salt; | |
} | |
/** | |
* Returns the user roles | |
* | |
* @return array The roles | |
*/ | |
public function getRoles() | |
{ | |
$roles = $this->roles; | |
// we need to make sure to have at least one role | |
$roles[] = StaticConst::ROLE_DEFAULT; | |
return array_unique($roles); | |
} | |
/** | |
* Never use this to check if this user has access to anything! | |
* | |
* Use the SecurityContext, or an implementation of AccessDecisionManager | |
* instead, e.g. | |
* | |
* $securityContext->isGranted('ROLE_USER'); | |
* | |
* @param string $role | |
* | |
* @return boolean | |
*/ | |
public function hasRole($role) | |
{ | |
return in_array(strtoupper($role), $this->getRoles(), true); | |
} | |
public function isEnabled() | |
{ | |
return $this->enabled; | |
} | |
public function isExpired() | |
{ | |
return !$this->isAccountNonExpired(); | |
} | |
/** | |
* @return bool | |
*/ | |
public function isLocked() | |
{ | |
return !$this->isAccountNonLocked(); | |
} | |
/** | |
* @return bool | |
*/ | |
public function isSuperAdmin() | |
{ | |
return $this->hasRole(StaticConst::ROLE_SUPER_ADMIN); | |
} | |
/** | |
* @param $role | |
* @return $this | |
*/ | |
public function removeRole($role) | |
{ | |
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) { | |
unset($this->roles[$key]); | |
$this->roles = array_values($this->roles); | |
} | |
return $this; | |
} | |
/** | |
* @param $boolean | |
* @return $this | |
*/ | |
public function setEnabled($boolean) | |
{ | |
$this->enabled = (Boolean) $boolean; | |
return $this; | |
} | |
/** | |
* Sets this user to expired. | |
* | |
* @param Boolean $boolean | |
* | |
* @return Utilisateurs | |
*/ | |
public function setExpired($boolean) | |
{ | |
$this->expired = (Boolean) $boolean; | |
return $this; | |
} | |
/** | |
* @param $boolean | |
* @return $this | |
*/ | |
public function setSuperAdmin($boolean) | |
{ | |
if (true === $boolean) { | |
$this->addRoles(StaticConst::ROLE_SUPER_ADMIN); | |
} else { | |
$this->removeRole(StaticConst::ROLE_SUPER_ADMIN); | |
} | |
return $this; | |
} | |
/** | |
* @param $boolean | |
* @return $this | |
*/ | |
public function setLocked($boolean) | |
{ | |
$this->locked = $boolean; | |
return $this; | |
} | |
/** | |
* @param array $roles | |
* @return $this | |
*/ | |
public function setRoles(array $roles) | |
{ | |
$this->roles = array(); | |
foreach ($roles as $role) { | |
$this->addRoles($role); | |
} | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return (string) $this->getMatricule(); | |
} | |
public function isAccountNonExpired() | |
{ | |
if (true === $this->expired) { | |
return false; | |
} | |
if (null !== $this->expiresAt && $this->expiresAt->getTimestamp() < time()) { | |
return false; | |
} | |
return true; | |
} | |
public function isAccountNonLocked() | |
{ | |
return !$this->locked; | |
} | |
/** | |
* @param \DateTime $date | |
* | |
* @return Utilisateurs | |
*/ | |
public function setExpiresAt(\DateTime $date = null) | |
{ | |
$this->expiresAt = $date; | |
return $this; | |
} | |
/** | |
* @return \DateTime | |
*/ | |
public function getExpiresAt(){ | |
return $this->expiresAt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment