src/CorporateTrainingBundle/Handler/LoginSyncDepartmentHandler.php line 37

Open in your IDE?
  1. <?php
  2. namespace CorporateTrainingBundle\Handler;
  3. use AppBundle\Common\ArrayToolkit;
  4. use Biz\User\CurrentUser;
  5. use Biz\User\Dao\UserProfileDao;
  6. use CorporateTrainingBundle\Biz\Post\Service\PostService;
  7. use CorporateTrainingBundle\Component\EIMClient\DepartmentFactory;
  8. use CorporateTrainingBundle\Component\EIMClient\UserFactory;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  12. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  13. class LoginSyncDepartmentHandler
  14. {
  15.     /**
  16.      * @var ContainerInterface
  17.      */
  18.     private $container;
  19.     /**
  20.      * @var Biz
  21.      */
  22.     private $biz;
  23.     public function __construct(ContainerInterface $container)
  24.     {
  25.         $this->container $container;
  26.         $this->biz $this->container->get('biz');
  27.     }
  28.     /**
  29.      * Do the magic.
  30.      */
  31.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  32.     {
  33.         $request $event->getRequest();
  34.         $userBind $this->canSyncUserWithReturnUserBind();
  35.         if (empty($userBind)) {
  36.             return true;
  37.         }
  38.         list($user$departmentIds) = $this->getUserSyncData($userBind);
  39.         $this->updateOrg($departmentIds);
  40.         $this->updateMobile($user);
  41.         $this->updatePost($user);
  42.         $this->updateHiredDate($user);
  43.         $this->updateAvatar($user);
  44.         $this->updateEmail($request$user);
  45.         $this->updateName($user);
  46.     }
  47.     protected function canSyncUserWithReturnUserBind()
  48.     {
  49.         $currentUser $this->biz['user'];
  50.         $userBinds $this->getUserService()->findBindsByUserId($currentUser['id']);
  51.         if (empty($userBinds)) {
  52.             return [];
  53.         }
  54.         $syncSetting $this->getSettingService()->get('sync_department_setting', []);
  55.         if (empty($syncSetting) || !$syncSetting['enable']) {
  56.             return [];
  57.         }
  58.         if (!in_array($syncSetting['type'], ['dingtalk''work_wechat''feishu'])) {
  59.             return [];
  60.         }
  61.         if (empty($syncSetting['autoUnlock'])) {
  62.             return [];
  63.         }
  64.         $userBind = [];
  65.         foreach ($userBinds as $userBind) {
  66.             if (!in_array($userBind['type'], ['dingtalk''work_wechat''feishu'])) {
  67.                 continue;
  68.             }
  69.             $userBind $userBind;
  70.         }
  71.         return $userBind;
  72.     }
  73.     protected function getUserSyncData($userBind)
  74.     {
  75.         $syncSetting $this->getSettingService()->get('sync_department_setting', []);
  76.         $userClient UserFactory::create($syncSetting);
  77.         $user = [];
  78.         $departmentIds = [];
  79.         if ('dingtalk' == $userBind['type']) {
  80.             $userId $userClient->getUserIdByUnionId($userBind['fromId']);
  81.             if (!$userId) {
  82.                 return;
  83.             }
  84.             $user $userClient->get($userId);
  85.             $departments = [];
  86.             $departmentClient DepartmentFactory::create($syncSetting);
  87.             foreach ($user['department'] as $departmentId) {
  88.                 array_push($departments$departmentClient->get($departmentId));
  89.             }
  90.             $departmentIds ArrayToolkit::column($departments'id');
  91.         }
  92.         if ('work_wechat' == $userBind['type']) {
  93.             $user $userClient->get($userBind['fromId']);
  94.             $departmentIds $user['department'];
  95.         }
  96.         if ('feishu' == $userBind['type']) {
  97.             $user $userClient->get($userBind['fromId']);
  98.             $departmentIds $user['department_ids'];
  99.         }
  100.         return [$user$departmentIds];
  101.     }
  102.     protected function updateOrg($departmentIds)
  103.     {
  104.         if (empty($departmentIds)) {
  105.             return true;
  106.         }
  107.         $currentUser $this->biz['user'];
  108.         $orgs $this->getOrgService()->findOrgsBySyncIds($departmentIds);
  109.         if (!empty($orgs)) {
  110.             if ($currentUser['orgIds'] != ArrayToolkit::column($orgs'id')) {
  111.                 $this->getUserDao()->update($currentUser['id'], [
  112.                         'orgIds' => ArrayToolkit::column($orgs'id'),
  113.                     ]
  114.                 );
  115.                 $this->getUserOrgService()->setUserOrgs($currentUser['id'], $orgs);
  116.             }
  117.             if ($currentUser['orgCodes'] != ArrayToolkit::column($orgs'orgCode')) {
  118.                 $this->getUserDao()->update($currentUser['id'], [
  119.                         'orgCodes' => ArrayToolkit::column($orgs'orgCode'),
  120.                     ]
  121.                 );
  122.             }
  123.         }
  124.     }
  125.     protected function updatePost($user)
  126.     {
  127.         $currentUser $this->biz['user'];
  128.         if (!empty($user['position'])) {
  129.             $post $this->getPostService()->getPostByName($user['position']);
  130.             if (isset($post)) {
  131.                 $this->getUserDao()->update($currentUser['id'], [
  132.                     'postId' => $post['id'],
  133.                 ]);
  134.             }
  135.         }
  136.     }
  137.     protected function updateMobile($user)
  138.     {
  139.         $currentUser $this->biz['user'];
  140.         if (!empty($user['mobile'])) {
  141.             $existedMobile $this->getUserService()->getUserByVerifiedMobile($user['mobile']);
  142.             if (!empty($existedMobile) && $existedMobile['id'] != $currentUser['id']) {
  143.                 $this->getUserDao()->update($existedMobile['id'], ['verifiedMobile' => '']);
  144.                 $this->getUserProfileDao()->update(['id' => $existedMobile['id']], ['mobile' => '']);
  145.                 $this->getUserDao()->update($currentUser['id'], ['verifiedMobile' => $user['mobile']]);
  146.                 $this->getUserProfileDao()->update(['id' => $currentUser['id']], ['mobile' => $user['mobile']]);
  147.             }
  148.         }
  149.     }
  150.     protected function updateHiredDate($user)
  151.     {
  152.         $currentUser $this->biz['user'];
  153.         if (!empty($user['hiredDate'])) {
  154.             $this->getUserDao()->update($currentUser['id'], [
  155.                 'hireDate' => $user['hiredDate'] / 1000,
  156.             ]);
  157.         }
  158.     }
  159.     protected function updateAvatar($user)
  160.     {
  161.         $currentUser $this->biz['user'];
  162.         if (isset($user['avatar']) && isset($user['thumb_avatar'])) {
  163.             $this->getUserDao()->update($currentUser['id'], [
  164.                 'smallAvatar' => $user['thumb_avatar'],
  165.                 'mediumAvatar' => $user['avatar'],
  166.                 'largeAvatar' => $user['avatar'],
  167.             ]);
  168.         }
  169.     }
  170.     protected function updateEmail($request$user)
  171.     {
  172.         $currentUser $this->biz['user'];
  173.         $user['email'] = isset($user['email']) ? trim($user['email']) : '';
  174.         if (!empty($user['email'])) {
  175.             $emailIsExist $this->getUserService()->getUserByEmail($user['email']);
  176.             if ($currentUser['email'] != $user['email'] && !$emailIsExist) {
  177.                 $user $this->getUserDao()->update($currentUser['id'], [
  178.                     'email' => $user['email'],
  179.                 ]);
  180.                 $this->kickUserLogout($currentUser['id']);
  181.                 $currentUser = new CurrentUser();
  182.                 $currentUser->fromArray($user);
  183.                 $this->switchUser($request$currentUser);
  184.             }
  185.         }
  186.     }
  187.     protected function updateName($user)
  188.     {
  189.         $currentUser $this->biz['user'];
  190.         if (!empty($user['name'])) {
  191.             $userProfile $this->getUserService()->getUserProfile($currentUser['id']);
  192.             if ($userProfile['truename'] != $user['name']) {
  193.                 $this->getUserProfileDao()->update($currentUser['id'], [
  194.                     'truename' => $user['name'],
  195.                 ]);
  196.             }
  197.         }
  198.     }
  199.     protected function kickUserLogout($userId)
  200.     {
  201.         $this->getSessionService()->clearByUserId($userId);
  202.         $tokens $this->getTokenService()->findTokensByUserIdAndType($userId'mobile_login');
  203.         if (!empty($tokens)) {
  204.             foreach ($tokens as $token) {
  205.                 $this->getTokenService()->destoryToken($token['token']);
  206.             }
  207.         }
  208.     }
  209.     /**
  210.      * switch current user.
  211.      *
  212.      * @param Request $request
  213.      *
  214.      * @return CurrentUser
  215.      */
  216.     protected function switchUser($requestCurrentUser $user)
  217.     {
  218.         $user['currentIp'] = $request->getClientIp();
  219.         $token = new UsernamePasswordToken($usernull'main'$user['roles']);
  220.         $this->container->get('security.token_storage')->setToken($token);
  221.         $biz $this->biz;
  222.         $biz['user'] = $user;
  223.         return $user;
  224.     }
  225.     /**
  226.      * @return UserDao
  227.      */
  228.     protected function getUserDao()
  229.     {
  230.         return $this->biz->dao('User:UserDao');
  231.     }
  232.     /**
  233.      * @return UserProfileDao
  234.      */
  235.     protected function getUserProfileDao()
  236.     {
  237.         return $this->biz->dao('User:UserProfileDao');
  238.     }
  239.     /**
  240.      * @return SettingService
  241.      */
  242.     protected function getSettingService()
  243.     {
  244.         return $this->biz->service('System:SettingService');
  245.     }
  246.     /**
  247.      * @return SessionService
  248.      */
  249.     protected function getSessionService()
  250.     {
  251.         return $this->biz->service('System:SessionService');
  252.     }
  253.     /**
  254.      * @return TokenService
  255.      */
  256.     protected function getTokenService()
  257.     {
  258.         return $this->biz->service('User:TokenService');
  259.     }
  260.     /**
  261.      * @return SettingService
  262.      */
  263.     protected function getUserService()
  264.     {
  265.         return $this->biz->service('User:UserService');
  266.     }
  267.     /**
  268.      * @return UserOrgService
  269.      */
  270.     protected function getUserOrgService()
  271.     {
  272.         return $this->biz->service('User:UserOrgService');
  273.     }
  274.     /**
  275.      * @return OrgService
  276.      */
  277.     protected function getOrgService()
  278.     {
  279.         return $this->biz->service('Org:OrgService');
  280.     }
  281.     /**
  282.      * @return PostService
  283.      */
  284.     protected function getPostService()
  285.     {
  286.         return $this->biz->service('CorporateTrainingBundle:Post:PostService');
  287.     }
  288. }