| 332 | | /** |
|---|
| 333 | | * Set user passord |
|---|
| 334 | | * |
|---|
| 335 | | * @param string $password |
|---|
| 336 | | */ |
|---|
| 337 | | public function setPassword($password) |
|---|
| 338 | | { |
|---|
| 339 | | if (!$password) |
|---|
| 340 | | { |
|---|
| 341 | | return; |
|---|
| 342 | | } |
|---|
| 343 | | |
|---|
| 344 | | $salt = md5(rand(100000, 999999).$this->getUsername()); |
|---|
| 345 | | $this->setSalt($salt); |
|---|
| 346 | | $algorithm = sfConfig::get('app_sf_guard_plugin_algorithm_callable', 'sha1'); |
|---|
| 347 | | $algorithmAsStr = is_array($algorithm) ? $algorithm[0].'::'.$algorithm[1] : $algorithm; |
|---|
| 348 | | if (!is_callable($algorithm)) |
|---|
| 349 | | { |
|---|
| 350 | | throw new sfException(sprintf('The algorithm callable "%s" is not callable.', $algorithmAsStr)); |
|---|
| 351 | | } |
|---|
| 352 | | $this->setAlgorithm($algorithmAsStr); |
|---|
| 353 | | |
|---|
| 354 | | parent::setPassword(call_user_func_array($algorithm, array($salt.$password))); |
|---|
| 355 | | } |
|---|
| 356 | | |
|---|
| 357 | | public function setPasswordBis($password) |
|---|
| 358 | | { |
|---|
| 359 | | } |
|---|
| 360 | | |
|---|
| 361 | | /** |
|---|
| 362 | | * Checks if given password matches current user one |
|---|
| 363 | | * |
|---|
| 364 | | * @param string $password |
|---|
| 365 | | * @return boolean |
|---|
| 366 | | */ |
|---|
| 367 | | public function checkPassword($password) |
|---|
| 368 | | { |
|---|
| 369 | | if ($callable = sfConfig::get('app_sf_guard_plugin_check_password_callable')) |
|---|
| 370 | | { |
|---|
| 371 | | return call_user_func_array($callable, array($this->getUsername(), $password)); |
|---|
| 372 | | } |
|---|
| 373 | | else |
|---|
| 374 | | { |
|---|
| 375 | | return $this->checkPasswordByGuard($password); |
|---|
| 376 | | } |
|---|
| 377 | | } |
|---|
| 378 | | |
|---|
| 379 | | /** |
|---|
| 380 | | * Checks password |
|---|
| 381 | | * |
|---|
| 382 | | * @param string $password |
|---|
| 383 | | * @return boolean |
|---|
| 384 | | */ |
|---|
| 385 | | public function checkPasswordByGuard($password) |
|---|
| 386 | | { |
|---|
| 387 | | $algorithm = $this->getAlgorithm(); |
|---|
| 388 | | if (false !== $pos = strpos($algorithm, '::')) |
|---|
| 389 | | { |
|---|
| 390 | | $algorithm = array(substr($algorithm, 0, $pos), substr($algorithm, $pos + 2)); |
|---|
| 391 | | } |
|---|
| 392 | | if (!is_callable($algorithm)) |
|---|
| 393 | | { |
|---|
| 394 | | throw new sfException(sprintf('The algorithm callable "%s" is not callable.', $algorithm)); |
|---|
| 395 | | } |
|---|
| 396 | | |
|---|
| 397 | | return $this->getPassword() == call_user_func_array($algorithm, array($this->getSalt().$password)); |
|---|
| 398 | | } |
|---|
| 399 | | |
|---|
| 400 | | public function getProfile() |
|---|
| 401 | | { |
|---|
| 402 | | if (!is_null($this->profile)) |
|---|
| 403 | | { |
|---|
| 404 | | return $this->profile; |
|---|
| 405 | | } |
|---|
| 406 | | |
|---|
| 407 | | $profileClass = sfConfig::get('app_sf_guard_plugin_profile_class', 'sfGuardUserProfile'); |
|---|
| 408 | | if (!class_exists($profileClass)) |
|---|
| 409 | | { |
|---|
| 410 | | throw new sfException(sprintf('The user profile class "%s" does not exist.', $profileClass)); |
|---|
| 411 | | } |
|---|
| 412 | | |
|---|
| 413 | | $fieldName = sfConfig::get('app_sf_guard_plugin_profile_field_name', 'user_id'); |
|---|
| 414 | | $profilePeerClass = $profileClass.'Peer'; |
|---|
| 415 | | |
|---|
| 416 | | // to avoid php segmentation fault |
|---|
| 417 | | class_exists($profilePeerClass); |
|---|
| 418 | | |
|---|
| 419 | | $foreignKeyColumn = call_user_func_array(array($profilePeerClass, 'translateFieldName'), array($fieldName, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME)); |
|---|
| 420 | | |
|---|
| 421 | | if (!$foreignKeyColumn) |
|---|
| 422 | | { |
|---|
| 423 | | throw new sfException(sprintf('The user profile class "%s" does not contain a "%s" column.', $profileClass, $fieldName)); |
|---|
| 424 | | } |
|---|
| 425 | | |
|---|
| 426 | | $c = new Criteria(); |
|---|
| 427 | | $c->add($foreignKeyColumn, $this->getId()); |
|---|
| 428 | | |
|---|
| 429 | | $this->profile = call_user_func_array(array($profileClass.'Peer', 'doSelectOne'), array($c)); |
|---|
| 430 | | |
|---|
| 431 | | if (!$this->profile) |
|---|
| 432 | | { |
|---|
| 433 | | $this->profile = new $profileClass(); |
|---|
| 434 | | if (method_exists($this->profile, 'setsfGuardUser')) |
|---|
| 435 | | { |
|---|
| 436 | | $this->profile->setsfGuardUser($this); |
|---|
| 437 | | } |
|---|
| 438 | | else |
|---|
| 439 | | { |
|---|
| 440 | | $method = 'set'.call_user_func_array(array($profilePeerClass, 'translateFieldName'), array($fieldName, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME)); |
|---|
| 441 | | $this->profile->$method($this->getId()); |
|---|
| 442 | | } |
|---|
| 443 | | } |
|---|
| 444 | | |
|---|
| 445 | | return $this->profile; |
|---|
| 446 | | } |
|---|
| 447 | | |
|---|
| 514 | | } |
|---|
| 515 | | |
|---|
| 516 | | public function addGroupByName($name, $con = null) |
|---|
| 517 | | { |
|---|
| 518 | | $group = sfGuardGroupPeer::retrieveByName($name); |
|---|
| 519 | | if (!$group) |
|---|
| 520 | | { |
|---|
| 521 | | throw new Exception(sprintf('The group "%s" does not exist.', $name)); |
|---|
| 522 | | } |
|---|
| 523 | | |
|---|
| 524 | | $ug = new sfGuardUserGroup(); |
|---|
| 525 | | $ug->setsfGuardUser($this); |
|---|
| 526 | | $ug->setGroupId($group->getId()); |
|---|
| 527 | | |
|---|
| 528 | | $ug->save($con); |
|---|
| 529 | | } |
|---|
| 530 | | |
|---|
| 531 | | public function addPermissionByName($name, $con = null) |
|---|
| 532 | | { |
|---|
| 533 | | $permission = sfGuardPermissionPeer::retrieveByName($name); |
|---|
| 534 | | if (!$permission) |
|---|
| 535 | | { |
|---|
| 536 | | throw new Exception(sprintf('The permission "%s" does not exist.', $name)); |
|---|
| 537 | | } |
|---|
| 538 | | |
|---|
| 539 | | $up = new sfGuardUserPermission(); |
|---|
| 540 | | $up->setsfGuardUser($this); |
|---|
| 541 | | $up->setPermissionId($permission->getId()); |
|---|
| 542 | | |
|---|
| 543 | | $up->save($con); |
|---|
| 544 | | } |
|---|
| 545 | | |
|---|
| 546 | | public function hasGroup($name) |
|---|
| 547 | | { |
|---|
| 548 | | if (!$this->groups) |
|---|
| 549 | | { |
|---|
| 550 | | $this->getGroups(); |
|---|
| 551 | | } |
|---|
| 552 | | |
|---|
| 553 | | return isset($this->groups[$name]); |
|---|
| 554 | | } |
|---|
| 555 | | |
|---|
| 556 | | public function getGroups() |
|---|
| 557 | | { |
|---|
| 558 | | if (!$this->groups) |
|---|
| 559 | | { |
|---|
| 560 | | $this->groups = array(); |
|---|
| 561 | | |
|---|
| 562 | | $c = new Criteria(); |
|---|
| 563 | | $c->add(sfGuardUserGroupPeer::USER_ID, $this->getId()); |
|---|
| 564 | | $ugs = sfGuardUserGroupPeer::doSelectJoinsfGuardGroup($c); |
|---|
| 565 | | |
|---|
| 566 | | foreach ($ugs as $ug) |
|---|
| 567 | | { |
|---|
| 568 | | $group = $ug->getsfGuardGroup(); |
|---|
| 569 | | $this->groups[$group->getName()] = $group; |
|---|
| 570 | | } |
|---|
| 571 | | } |
|---|
| 572 | | |
|---|
| 573 | | return $this->groups; |
|---|
| 574 | | } |
|---|
| 575 | | |
|---|
| 576 | | public function getGroupNames() |
|---|
| 577 | | { |
|---|
| 578 | | return array_keys($this->getGroups()); |
|---|
| 579 | | } |
|---|
| 580 | | |
|---|
| 581 | | public function hasPermission($name) |
|---|
| 582 | | { |
|---|
| 583 | | if (!$this->permissions) |
|---|
| 584 | | { |
|---|
| 585 | | $this->getPermissions(); |
|---|
| 586 | | } |
|---|
| 587 | | |
|---|
| 588 | | return isset($this->permissions[$name]); |
|---|
| 589 | | } |
|---|
| 590 | | |
|---|
| 591 | | public function getPermissions() |
|---|
| 592 | | { |
|---|
| 593 | | if (!$this->permissions) |
|---|
| 594 | | { |
|---|
| 595 | | $this->permissions = array(); |
|---|
| 596 | | |
|---|
| 597 | | $c = new Criteria(); |
|---|
| 598 | | $c->add(sfGuardUserPermissionPeer::USER_ID, $this->getId()); |
|---|
| 599 | | $ups = sfGuardUserPermissionPeer::doSelectJoinsfGuardPermission($c); |
|---|
| 600 | | |
|---|
| 601 | | foreach ($ups as $up) |
|---|
| 602 | | { |
|---|
| 603 | | $permission = $up->getsfGuardPermission(); |
|---|
| 604 | | $this->permissions[$permission->getName()] = $permission; |
|---|
| 605 | | } |
|---|
| 606 | | } |
|---|
| 607 | | |
|---|
| 608 | | return $this->permissions; |
|---|
| 609 | | } |
|---|
| 610 | | |
|---|
| 611 | | public function getPermissionNames() |
|---|
| 612 | | { |
|---|
| 613 | | return array_keys($this->getPermissions()); |
|---|
| 614 | | } |
|---|
| 615 | | |
|---|
| 616 | | // merge of permission in a group + permissions |
|---|
| 617 | | public function getAllPermissions() |
|---|
| 618 | | { |
|---|
| 619 | | if (!$this->allPermissions) |
|---|
| 620 | | { |
|---|
| 621 | | $this->allPermissions = $this->getPermissions(); |
|---|
| 622 | | |
|---|
| 623 | | foreach ($this->getGroups() as $group) |
|---|
| 624 | | { |
|---|
| 625 | | foreach ($group->getsfGuardGroupPermissions() as $gp) |
|---|
| 626 | | { |
|---|
| 627 | | $permission = $gp->getsfGuardPermission(); |
|---|
| 628 | | |
|---|
| 629 | | $this->allPermissions[$permission->getName()] = $permission; |
|---|
| 630 | | } |
|---|
| 631 | | } |
|---|
| 632 | | } |
|---|
| 633 | | |
|---|
| 634 | | return $this->allPermissions; |
|---|
| 635 | | } |
|---|
| 636 | | |
|---|
| 637 | | public function getAllPermissionNames() |
|---|
| 638 | | { |
|---|
| 639 | | return array_keys($this->getAllPermissions()); |
|---|
| 640 | | } |
|---|
| 641 | | |
|---|
| 642 | | public function reloadGroupsAndPermissions() |
|---|
| 643 | | { |
|---|
| 644 | | $this->groups = null; |
|---|
| 645 | | $this->permissions = null; |
|---|
| 646 | | $this->allPermissions = null; |
|---|