Skip to content

@gkalyan /CompanyController.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Prob deleting users
public function deleteUserAction($id) {
$em = $this->getDoctrine()->getManager();
$parentUser = new User();
$user = $em->getRepository('ACMECompanyBundle:User')->find($id);
$currentUser = $this->get('security.context')->getToken()->getUser();
if ($id == $currentUser->getId()) {
return new Response("You cannot delete the current user");
}
if (!$user) {
throw $this->createNotFoundException('No user found for id '.$id);
}
if ($user->getParentId() != null) {
$parentUser = $em->getRepository('ACMECompanyBundle:User')->find($user->getParentId());
$parentUser->removeChildren($user);
$em->persist($parentUser);
}
try {
$em->remove($user);
$em->flush();
$msg = "User deleted!";
$code = "OK";
} catch (DBALException $e) {
return new Response($e);
$msg = "User cannot be deleted!";
$code = "ERR";
}
$response = new Response(json_encode(array('code' => $code, 'msg' => $msg)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
//Role Entity
<?php
namespace ACME\CompanyBundle\Entity;
use ACME\CompanyBundle\Entity\Role;
use ACME\CompanyBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping as ORM;
/**
* ACME\CompanyBundle\Entity\Role
*
* @ORM\Table(name="roles")
* @ORM\Entity
*/
class Role implements RoleInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="smallint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $roleName
*
* @ORM\Column(name="role_name", type="string", length=20)
*/
protected $roleName;
/**
* @var string $roleAlt
*
* @ORM\Column(name="role_alt", type="string", length=35, unique=true)
*/
protected $roleAlt;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="userRoles", cascade={"persist"})
*/
protected $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set role_name
*
* @param string $roleName
* @return Role
*/
public function setRoleName($roleName)
{
$this->roleName = $roleName;
return $this;
}
/**
* Get role_name
*
* @return string
*/
public function getRoleName()
{
return $this->roleName;
}
/**
* Add users
*
* @param User $users
* @return Role
*/
public function addUser(User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* @param User $users
*/
public function removeUser(User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* @return Collection
*/
public function getUsers()
{
return $this->users;
}
/**
* Implementation of getRole for the RoleInterface.
*
* @return string The role.
*/
public function getRole()
{
return $this->getRoleName();
}
/**
* Set role_alt
*
* @param string $roleAlt
* @return Role
*/
public function setRoleAlt($roleAlt)
{
$this->roleAlt = $roleAlt;
return $this;
}
/**
* Get role_alt
*
* @return string
*/
public function getRoleAlt()
{
return $this->roleAlt;
}
public static function loadValidatorMetadata(ClassMetadata $metadata) {
$metadata->addConstraint(new UniqueEntity(array(
"fields" => "roleAlt",
"message" => "This role name is already in use"
)
));
}
}
//User Entity
<?php
namespace ACME\CompanyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* ACME\CompanyBundle\Entity\User
*
* @ORM\Table(name="users")
* @ORM\Entity
*/
class User implements UserInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="smallint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=20, unique=TRUE)
*/
protected $username;
/**
* @var string $password
*
* @ORM\Column(name="password", type="string", length=255)
*/
protected $password;
/**
* @var string $salt
*
* @ORM\Column(name="salt", type="string", length=255)
*/
protected $salt;
/**
* @var string $fullName
*
* @ORM\Column(name="full_name", type="string", length=60, unique=TRUE)
*/
protected $fullName;
/**
* @var integer $contactId
*
* @ORM\Column(name="contact_id", type="integer")
*/
protected $contactId;
/**
* @var integer $parentId
*
* @ORM\Column(name="parent_id", type="smallint", nullable=true)
*/
protected $parentId;
/**
* @var boolean $isActive
*
* @ORM\Column(name="is_active", type="boolean")
*/
protected $isActive;
/**
* @var \DateTime $doj
*
* @ORM\Column(name="doj", type="date", nullable=TRUE)
*/
protected $doj;
/**
* @var \DateTime $createdAt
*
* @ORM\Column(name="created_at", type="datetime")
*/
protected $createdAt;
/**
* @var \DateTime $updatedAt
*
* @ORM\Column(name="updated_at", type="datetime", nullable=TRUE)
*/
protected $updatedAt;
/**
* @var \DateTime $lastAccessed
*
* @ORM\Column(name="last_accessed", type="datetime", nullable=TRUE)
*/
protected $lastAccessed;
/**
* @ORM\ManyToOne(targetEntity="Contact", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $contact;
/**
* @ORM\ManyToMany(targetEntity="Branch", inversedBy="users", cascade={"persist"})
* @ORM\JoinTable(name="users_branches")
*
* @var ArrayCollection $branches
*/
protected $branches;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users", cascade={"persist"})
* @ORM\JoinTable(name="users_roles")
*
* @var ArrayCollection $userRoles
*/
protected $userRoles;
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="parent", cascade={"persist"})
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $parent;
public function __construct()
{
$this->parentId = null; // Default value for column parent_id
$this->doj = null;
$this->lastAccessed = null;
$this->updatedAt = null;
$this->isActive = true;
$this->branches = new ArrayCollection();
$this->userRoles = new ArrayCollection();
$this->children = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* @param string salt
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set full_name
*
* @param string $fullName
* @return User
*/
public function setFullName($fullName)
{
$this->fullName = $fullName;
return $this;
}
/**
* Get full_name
*
* @return string
*/
public function getFullName()
{
return $this->fullName;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set contact_id
*
* @param integer $contactId
* @return User
*/
public function setContactId($contactId)
{
$this->contactId = $contactId;
return $this;
}
/**
* Get contact_id
*
* @return integer
*/
public function getContactId()
{
return $this->contactId;
}
/**
* Set parent_id
*
* @param integer $parentId
* @return User
*/
public function setParentId($parentId)
{
$this->parentId = $parentId;
return $this;
}
/**
* Get parent_id
*
* @return integer
*/
public function getParentId()
{
return $this->parentId;
}
/**
* Set doj
*
* @param \DateTime $doj
* @return User
*/
public function setDoj($doj)
{
$this->doj = $doj;
return $this;
}
/**
* Get doj
*
* @return \DateTime
*/
public function getDoj()
{
return $this->doj;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return User
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updated_at
*
* @param \DateTime $updatedAt
* @return User
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updated_at
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set last_accessed
*
* @param \DateTime $lastAccessed
* @return User
*/
public function setLastAccessed($lastAccessed)
{
$this->lastAccessed = $lastAccessed;
return $this;
}
/**
* Get last_accessed
*
* @return \DateTime
*/
public function getLastAccessed()
{
return $this->lastAccessed;
}
/**
* Add branch
*
* @param ACME\CompanyBundle\Entity\Branch $branch
* @return User
*/
public function addBranch(\ACME\CompanyBundle\Entity\Branch $branch)
{
$this->branches[] = $branch;
return $this;
}
/**
* Remove branch
*
* @param ACME\CompanyBundle\Entity\Branch $branch
*/
public function removeBranch(\ACME\CompanyBundle\Entity\Branch $branch)
{
$this->branches->removeElement($branch);
}
/**
* Get branches
*
* @return Doctrine\Common\Collections\Collection
*/
public function getBranches()
{
return $this->branches;
}
/**
* Get branches
*
* @return array An array of branches
*/
public function getBranchesArray()
{
return $this->branches->toArray();
}
/**
* Get children
*
* @return array An array of children
*/
public function getChildren()
{
return $this->children->toArray();
}
/**
* Set parent
*
* @param ACME\CompanyBundle\Entity\User $parent
* @return User
*/
public function setParent(\ACME\CompanyBundle\Entity\User $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return ACME\CompanyBundle\Entity\User
*/
public function getParent()
{
return $this->parent;
}
/**
* Set isActive
*
* @param boolean $isActive
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set contact
*
* @param ACME\CompanyBundle\Entity\Contact $contact
* @return User
*/
public function setContact(\ACME\CompanyBundle\Entity\Contact $contact = null)
{
$this->contact = $contact;
return $this;
}
/**
* Get contact
*
* @return ACME\CompanyBundle\Entity\Contact
*/
public function getContact()
{
return $this->contact;
}
/**
* Erases the user credentials.
*/
public function eraseCredentials()
{
}
/**
* Add userRole
*
* @param ACME\CompanyBundle\Entity\Role $userRole
* @return User
*/
public function addUserRole(\ACME\CompanyBundle\Entity\Role $userRole)
{
$this->userRoles[] = $userRole;
return $this;
}
/**
* Remove userRole
*
* @param ACME\CompanyBundle\Entity\Role $userRole
*/
public function removeUserRole(\ACME\CompanyBundle\Entity\Role $userRole)
{
$this->userRoles->removeElement($userRole);
}
/**
* Get userRoles
*
* @return Doctrine\Common\Collections\Collection
*/
public function getUserRoles()
{
return $this->userRoles;
}
/**
* Gets an array of roles.
*
* @return array An array of Role objects
*/
public function getRoles()
{
return $this->getUserRoles()->toArray();
}
/**
* Add children
*
* @param ACME\CompanyBundle\Entity\User $children
* @return User
*/
public function addChildren(\ACME\CompanyBundle\Entity\User $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* @param ACME\CompanyBundle\Entity\User $children
*/
public function removeChildren(\ACME\CompanyBundle\Entity\User $children)
{
$this->children->removeElement($children);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.