BKController.class.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace KIF\Core;
  3. use Cas\Module\BackPassport;
  4. /**
  5. * 继承自MVC里的Controller,封装了后台用户登录状态、用户信息等一些方法
  6. * @author lishumingoo@gmail.com
  7. */
  8. class BKController extends \KIF\Core\Controller {
  9. /**
  10. * 判断用户登录状态
  11. * @return Boolean
  12. */
  13. static public function isLogin() {
  14. return (boolean) self::getUid();
  15. }
  16. /**
  17. * 要求未登陆用户去登陆
  18. * @return boolean
  19. */
  20. static public function requireLogin() {
  21. if (!self::isLogin()) {
  22. $referer = Request::url();
  23. $url = Request::schemeDomain() . '/?c=Admin_BackUser&a=Login&referer=' . urlencode($referer);
  24. parent::redirect($url);
  25. }
  26. return true;
  27. }
  28. /**
  29. * 获取用户id
  30. * @return false | int
  31. */
  32. static public function getUid() {
  33. $user = self::getUser();
  34. if (!$user) {
  35. return false;
  36. }
  37. return $user['uid'];
  38. }
  39. /**
  40. * 获取用户名
  41. * 这里的用户名是不靠谱的!!!
  42. * @return string
  43. */
  44. static public function getUsername() {
  45. $user = self::getUser();
  46. if (!$user) {
  47. return false;
  48. }
  49. return $user['name'];
  50. }
  51. /**
  52. *
  53. * 从Cookie中获取用户的登陆信息
  54. * @return boolean | array
  55. */
  56. public function getUser() {
  57. static $user = null;
  58. if ($user) {
  59. return $user;
  60. }
  61. $user = array();
  62. $objBackPassport = new BackPassport();
  63. $tmpResult = $objBackPassport->getLoginInfo();
  64. if ($tmpResult->isSuccess()) {
  65. $user = $tmpResult->getData();
  66. }
  67. return $user;
  68. }
  69. public function run() {
  70. $action = $this->action;
  71. $this->$action();
  72. }
  73. }