From 3ac46081551b09cb1a6d351f89e7dab95f7667d4 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Fri, 28 Oct 2022 11:54:38 +0200 Subject: [PATCH] on fait un peu de cleaning --- How to install Matomo.html | 8 - .../Repository/UserRepository.php | 247 ------------------ .../plugins/UsersManager/Validators/Email.php | 66 ----- .../plugins/UsersManager/Validators/Login.php | 72 ----- 4 files changed, 393 deletions(-) delete mode 100644 How to install Matomo.html delete mode 100644 matomo2/plugins/UsersManager/Repository/UserRepository.php delete mode 100644 matomo2/plugins/UsersManager/Validators/Email.php delete mode 100644 matomo2/plugins/UsersManager/Validators/Login.php diff --git a/How to install Matomo.html b/How to install Matomo.html deleted file mode 100644 index 4895edd..0000000 --- a/How to install Matomo.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - -You will be redirected to the Matomo Analytics Installation documentation on matomo.org/docs/installation - - diff --git a/matomo2/plugins/UsersManager/Repository/UserRepository.php b/matomo2/plugins/UsersManager/Repository/UserRepository.php deleted file mode 100644 index 59f1946..0000000 --- a/matomo2/plugins/UsersManager/Repository/UserRepository.php +++ /dev/null @@ -1,247 +0,0 @@ -model = $model; - $this->filter = $filter; - $this->password = $password; - } - - /** - * @param string $userLogin - * @param string $email - * @param int $initialIdSite - * @param string $password - * @param bool $isPasswordHashed - * @throws \Exception - */ - public function create( - string $userLogin, - string $email, - ?int $initialIdSite = null, - string $password = '', - bool $isPasswordHashed = false - ): void { - - - if (!Piwik::hasUserSuperUserAccess()) { - // check if the user has admin access to the site - Piwik::checkUserHasAdminAccess($initialIdSite); - } - - BaseValidator::check(Piwik::translate('General_Username'), $userLogin, [new Login(true)]); - BaseValidator::check(Piwik::translate('Installation_Email'), $email, [new Email(true)]); - - if (!empty($password)) { - if (!$isPasswordHashed) { - $passwordTransformed = UsersManager::getPasswordHash($password); - } else { - $passwordTransformed = $password; - } - $password = $this->password->hash($passwordTransformed); - } - - $this->model->addUser($userLogin, $password, $email, Date::now()->getDatetime()); - - if ($initialIdSite) { - API::getInstance()->setUserAccess($userLogin, 'view', $initialIdSite); - } - - $this->sendUserCreationNotification($userLogin); - } - - public function inviteUser(string $userLogin, string $email, ?int $initialIdSite = null, $expiryInDays = null): void - { - $this->create($userLogin, $email, $initialIdSite); - $this->model->updateUserFields($userLogin, ['invited_by' => Piwik::getCurrentUserLogin()]); - $user = $this->model->getUser($userLogin); - $generatedToken = $this->model->generateRandomInviteToken(); - $this->model->attachInviteToken($userLogin, $generatedToken, $expiryInDays); - $this->sendInvitationEmail($user, $generatedToken, $expiryInDays); - } - - public function reInviteUser(string $userLogin, $expiryInDays = null): void - { - $user = $this->model->getUser($userLogin); - $generatedToken = $this->model->generateRandomInviteToken(); - $this->model->attachInviteToken($userLogin, $generatedToken, $expiryInDays); - $this->sendInvitationEmail($user, $generatedToken, $expiryInDays); - } - - protected function sendUserCreationNotification(string $createdUserLogin): void - { - $mail = StaticContainer::getContainer()->make(UserCreatedEmail::class, [ - 'login' => Piwik::getCurrentUserLogin(), - 'emailAddress' => Piwik::getCurrentUserEmail(), - 'userLogin' => $createdUserLogin, - ]); - $mail->safeSend(); - } - - protected function sendInvitationEmail(array $user, string $inviteToken, int $expiryInDays): void - { - $site = $this->model->getSitesAccessFromUser($user['login']); - - if (isset($site[0])) { - $siteName = Site::getNameFor($site[0]['site']); - } else { - $siteName = "Default Site"; - } - - $email = StaticContainer::getContainer()->make(UserInviteEmail::class, [ - 'currentUser' => Piwik::getCurrentUserLogin(), - 'invitedUser' => $user, - 'siteName' => $siteName, - 'token' => $inviteToken, - 'expiryInDays' => $expiryInDays - ]); - $email->safeSend(); - } - - /** - * @param array $user - * @return array - * @throws \Exception - */ - public function enrichUser(array $user): array - { - if (empty($user)) { - return $user; - } - - unset($user['token_auth']); - unset($user['password']); - unset($user['ts_password_modified']); - unset($user['idchange_last_viewed']); - - if ($lastSeen = LastSeenTimeLogger::getLastSeenTimeForUser($user['login'])) { - $user['last_seen'] = Date::getDatetimeFromTimestamp($lastSeen); - } - - $user['invite_status'] = 'active'; - - if (!empty($user['invite_expired_at'])) { - $inviteExpireAt = Date::factory($user['invite_expired_at']); - // if token expired - if (Date::now()->isLater($inviteExpireAt)) { - $user['invite_status'] = 'expired'; - } - // if token not expired - if (Date::now()->isEarlier($inviteExpireAt)) { - $dayLeft = floor(Date::secondsToDays($inviteExpireAt->getTimestamp() - Date::now()->getTimestamp())); - $user['invite_status'] = $dayLeft; - } - } - - if (Piwik::hasUserSuperUserAccess()) { - $user['uses_2fa'] = !empty($user['twofactor_secret']) && $this->isTwoFactorAuthPluginEnabled(); - unset($user['twofactor_secret']); - return $user; - } - - $newUser = ['login' => $user['login']]; - - if ($user['login'] === Piwik::getCurrentUserLogin() || !empty($user['superuser_access'])) { - $newUser['email'] = $user['email']; - } - - if (isset($user['role'])) { - $newUser['role'] = $user['role'] == 'superuser' ? 'admin' : $user['role']; - } - if (isset($user['capabilities'])) { - $newUser['capabilities'] = $user['capabilities']; - } - - if (isset($user['superuser_access'])) { - $newUser['superuser_access'] = $user['superuser_access']; - } - - if (isset($user['last_seen'])) { - $newUser['last_seen'] = $user['last_seen']; - } - $newUser['invite_status'] = $user['invite_status']; - if (isset($user['invited_by'])) { - $newUser['invited_by'] = $user['invited_by']; - } - - return $newUser; - } - - /** - * @param array $users - * @return mixed - * @throws \Exception - */ - public function enrichUsers(array $users): array - { - if (!empty($users)) { - foreach ($users as $index => $user) { - $users[$index] = $this->enrichUser($user); - } - } - return $users; - } - - /** - * @param array $users - * @return mixed - */ - public function enrichUsersWithLastSeen(array $users): array - { - $formatter = new Formatter(); - - $lastSeenTimes = LastSeenTimeLogger::getLastSeenTimesForAllUsers(); - foreach ($users as &$user) { - $login = $user['login']; - if (isset($lastSeenTimes[$login])) { - $user['last_seen'] = $formatter->getPrettyTimeFromSeconds(time() - $lastSeenTimes[$login]); - } - } - return $users; - } - - private function isTwoFactorAuthPluginEnabled(): bool - { - if (!isset($this->twoFaPluginActivated)) { - $this->twoFaPluginActivated = Plugin\Manager::getInstance()->isPluginActivated('TwoFactorAuth'); - } - return $this->twoFaPluginActivated; - } -} diff --git a/matomo2/plugins/UsersManager/Validators/Email.php b/matomo2/plugins/UsersManager/Validators/Email.php deleted file mode 100644 index 4539c77..0000000 --- a/matomo2/plugins/UsersManager/Validators/Email.php +++ /dev/null @@ -1,66 +0,0 @@ -checkUnique = $checkUnique; - $this->userLogin = $userLogin; - } - - public function validate($value) - { - if ($this->isValueBare($value)) { - return; - } - - if (!Piwik::isValidEmailString($value)) { - throw new Exception(Piwik::translate('General_ValidatorErrorNotEmailLike', [$value])); - } - - if ($this->checkUnique) { - $this->isUnique($value); - } - } - - /** - * check if email already exist in database - * @param $email - * @throws \Exception - */ - private function isUnique($email) - { - if (APIUsersManager::getInstance()->userEmailExists($email)) { - throw new Exception(Piwik::translate('UsersManager_ExceptionEmailExists', $email)); - } - - if ($this->userLogin && mb_strtolower($this->userLogin) !== mb_strtolower($email) && APIUsersManager::getInstance()->userExists($email)) { - throw new Exception(Piwik::translate('UsersManager_ExceptionEmailExistsAsLogin', $email)); - } - - if (!$this->userLogin && APIUsersManager::getInstance()->userExists($email)) { - throw new Exception(Piwik::translate('UsersManager_ExceptionEmailExistsAsLogin', $email)); - } - - if (!Piwik::isValidEmailString($email)) { - throw new Exception(Piwik::translate('UsersManager_ExceptionInvalidEmail')); - } - } -} diff --git a/matomo2/plugins/UsersManager/Validators/Login.php b/matomo2/plugins/UsersManager/Validators/Login.php deleted file mode 100644 index ad97537..0000000 --- a/matomo2/plugins/UsersManager/Validators/Login.php +++ /dev/null @@ -1,72 +0,0 @@ -checkUnique = $checkUnique; - } - - public function validate($value) - { - if ( - !SettingsPiwik::isUserCredentialsSanityCheckEnabled() - && !empty($value) - ) { - return; - } - - $l = strlen($value); - if ( - !($l >= self::loginMinimumLength - && $l <= self::loginMaximumLength - && (preg_match('/^[A-Za-zÄäÖöÜüß0-9_.@+-]*$/D', $value) > 0)) - ) { - throw new Exception(Piwik::translate( - 'UsersManager_ExceptionInvalidLoginFormat', - [self::loginMinimumLength, self::loginMaximumLength] - )); - } - - if ($this->checkUnique) { - $this->isUnique($value); - } - } - - /** - * check if login already exist in database - * @param $login - * @throws \Exception - */ - private function isUnique($login) - { - if (APIUsersManager::getInstance()->userExists($login)) { - throw new Exception(Piwik::translate('UsersManager_ExceptionLoginExists', $login)); - } - - if (APIUsersManager::getInstance()->userEmailExists($login)) { - throw new Exception(Piwik::translate('UsersManager_ExceptionLoginExistsAsEmail', $login)); - } - } -}