/** * Attempts to log the authenticated CAS user into Drupal. * * This method should be used to login a user after they have successfully * authenticated with the CAS server. * * @param CasPropertyBag $property_bag * CasPropertyBag containing username and attributes from CAS. * * @throws CasLoginException */ public function loginToDrupal(CasPropertyBag $property_bag, $ticket) { $this->eventDispatcher->dispatch(CasHelper::CAS_PROPERTY_ALTER, new CasPropertyEvent($property_bag)); $account = $this->userLoadByName($property_bag->getUsername()); if (!$account) { $config = $this->settings->get('cas.settings'); if ($config->get('user_accounts.auto_register') === TRUE) { if (!$property_bag->getRegisterStatus()) { $_SESSION['cas_temp_disable'] = TRUE; throw new CasLoginException("Cannot register user, an event listener denied access."); } $account = $this->registerUser($property_bag->getUsername()); } else { throw new CasLoginException("Cannot login, local Drupal user account does not exist."); } } $this->eventDispatcher->dispatch(CasHelper::CAS_USER_ALTER, new CasUserEvent($account, $property_bag)); $account->save(); if (!$property_bag->getLoginStatus()) { $_SESSION['cas_temp_disable'] = TRUE; throw new CasLoginException("Cannot login, an event listener denied access."); } $this->userLoginFinalize($account); $this->storeLoginSessionData($this->sessionManager->getId(), $ticket); }
/** * Test generating exception when listeners deny access. * * @covers ::loginToDrupal * @covers ::__construct * * @dataProvider loginToDrupalListenerDeniedDataProvider */ public function testLoginToDrupalListenerDenied(CasPropertyBag $property_bag, $exception_message) { $config_factory = $this->getConfigFactoryStub(array('cas.settings' => array('user_accounts.auto_register' => TRUE))); $cas_login = $this->getMockBuilder('Drupal\\cas\\Service\\CasLogin')->setMethods(array('userLoadByName', 'userLoginFinalize', 'storeLoginSessionData'))->setConstructorArgs(array($config_factory, $this->entityManager, $this->sessionManager, $this->connection, $this->eventDispatcher))->getMock(); $account = $this->getMockBuilder('Drupal\\user\\UserInterface')->disableOriginalConstructor()->getMock(); $cas_login->expects($this->any())->method('userLoadByName')->will($this->returnValue($property_bag->getRegisterStatus() ? $account : FALSE)); $_SESSION['cas_temp_disable'] = FALSE; $ticket = $this->randomMachineName(24); $this->setExpectedException('\\Drupal\\cas\\Exception\\CasLoginException', $exception_message); $cas_login->loginToDrupal($property_bag, $ticket); $this->assertEquals('TRUE', $_SESSION['cas_temp_disable']); }