View source
<?php
namespace Drupal\force_password_change\Service;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\force_password_change\Mapper\ForcePasswordChangeMapperInterface;
use Drupal\user\UserDataInterface;
class ForcePasswordChangeService implements ForcePasswordChangeServiceInterface {
protected $mapper;
protected $currentUser;
protected $configFactory;
protected $userData;
public function __construct(ForcePasswordChangeMapperInterface $mapper, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory, UserDataInterface $userData) {
$this->mapper = $mapper;
$this->currentUser = $currentUser;
$this->configFactory = $configFactory;
$this->userData = $userData;
}
public function forceUsersPasswordChange($uids = []) {
if (!count($uids)) {
$uids = $this->mapper
->getActiveUserIds();
}
foreach ($uids as $uid) {
$this
->forceUserPasswordChange($uid);
$this
->registerForcePasswordTime($uid);
}
}
public function forceUserPasswordChange($uid) {
$this->userData
->set('force_password_change', $uid, 'pending_force', 1);
}
public function registerForcePasswordTime($uid) {
$this->userData
->set('force_password_change', $uid, 'last_force', $this
->getRequestTime());
}
public function checkForForce() {
$redirect = FALSE;
if ($this->userData
->get('force_password_change', $this->currentUser
->id(), 'pending_force')) {
$redirect = 'admin_forced';
}
elseif ($this->configFactory
->get('force_password_change.settings')
->get('expire_password')) {
$last_change = $this->userData
->get('force_password_change', $this->currentUser
->id(), 'last_change');
$created = $this->mapper
->getUserCreatedTime($this->currentUser
->id());
$expiry = $this->mapper
->getExpiryTimeFromRoles($this->currentUser
->getRoles());
if ($expiry && ($last_change && $this
->getRequestTime() - $expiry > $last_change) || !$last_change && $this
->getRequestTime() - $expiry > $created) {
$this->userData
->set('force_password_change', $this->currentUser
->id(), 'pending_force', 1);
$redirect = 'expired';
}
}
return $redirect;
}
public function getLastChangeForRole($rid) {
return $this->mapper
->getLastChangeForRole($rid);
}
public function updateLastChangeForRoles($rids) {
$this->mapper
->updateLastChangeForRoles($rids);
}
public function insertExpiryForRoles($values) {
$this->mapper
->insertExpiryForRoles($values);
}
public function updateExpiryForRole($rid, $time_period, $weight) {
$this->mapper
->updateExpiryForRole($rid, $time_period, $weight);
}
public function getUserCountForRole($rid) {
$rid = $rid == 'authenticated' ? FALSE : $rid;
return $this->mapper
->getUserCountForRole($rid);
}
public function getPendingUsersForRole($rid, $countQuery = FALSE) {
$rid = $rid == 'authenticated' ? FALSE : $rid;
$uids = $this->mapper
->getPendingUserIds($rid);
if ($countQuery) {
return count($uids);
}
return $this
->userLoadMultiple($uids);
}
public function getNonPendingUsersForRole($rid) {
$rid = $rid == 'authenticated' ? FALSE : $rid;
$uids = $this->mapper
->getNonPendingUserIds($rid);
return $this
->userLoadMultiple($uids);
}
public function getRoleExpiryTimePeriods() {
return $this->mapper
->getRoleExpiryTimePeriods();
}
public function getUsersForRole($rid, $uidOnly = TRUE) {
$uids = $this->mapper
->getUserIdsForRole($rid);
if ($uidOnly) {
return $uids;
}
return $this
->userLoadMultiple($uids);
}
public function setChangedTimeForUser($uid) {
$this->userData
->set('force_password_change', $uid, 'last_change', $this
->getRequestTime());
}
public function removePendingForce($uid) {
$this->userData
->set('force_password_change', $uid, 'pending_force', 0);
}
public function getTextDate($seconds) {
$year = 60 * 60 * 24 * 365;
if ($timestamp % $year === 0) {
$time_period = $timestamp / $year;
$time_period = $time_period > 1 ? $time_period . ' ' . t('years') : t('year');
}
else {
$week = 60 * 60 * 24 * 7;
if ($timestamp % $week === 0) {
$time_period = $timestamp / $week;
$time_period = $time_period > 1 ? $time_period . ' ' . t('weeks') : t('week');
}
else {
$day = 60 * 60 * 24;
if ($timestamp % $day === 0) {
$time_period = $timestamp / $day;
$time_period = $time_period > 1 ? $time_period . ' ' . t('days') : t('day');
}
else {
$hour = 60 * 60;
if ($timestamp % $hour === 0) {
$time_period = $timestamp / $hour;
$time_period = $time_period > 1 ? $time_period . ' ' . t('hours') : t('hour');
}
}
}
}
return $time_period;
}
public function addFirstTimeLogin($uid) {
$this->mapper
->addFirstTimeLogin($uid);
}
public function removeFirstTimeLogin($uid) {
$this->mapper
->removeFirstTimeLogin($uid);
}
public function getFirstTimeLoginUids() {
return $this->mapper
->getFirstTimeLoginUids();
}
protected function userLoadMultiple(array $uids) {
return user_load_multiple($uids);
}
protected function getRequestTime() {
return REQUEST_TIME;
}
}