public
Created

Prob deleting users

  • Download Gist
CompanyController.php
PHP
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
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.php
PHP
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
//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.php
PHP
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
//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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.