Commit 50c007b9 authored by Dainis Abols's avatar Dainis Abols
Browse files

Composer version with `luitd/lu-ldap-authorization` dependency

Version 4.2.0
parents
Pipeline #116 failed with stages
in 0 seconds
.idea/
vendor
<?php
namespace Lu\LuAuth\Models;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class Groups. Makes requests for groups.
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0
* @since 28.05.2018
*
* @package Lu\LuAuth\Models
*/
class Groups
{
/**
* Table name
*
* @var string
*/
protected $table = 'be_groups';
/**
* Set request type
*
* @var string
*/
protected $type = 'be';
/**
* Used username
*
* @var string
*/
private $username;
/**
* Users constructor. Set initial usage.
*
* @param string $type 'be' or 'fe'
*/
public function __construct(string $type)
{
// Set config value
$this->conf = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)->get('lu_auth');
// Set used group
if ($type == 'fe') {
$this->table = 'fe_groups';
} else {
$this->table = 'be_groups';
}
// Set used group
$this->type = $type == 'fe' ? 'fe' : 'be';
}
/**
* Set username
*
* @param string $username
*/
public function setUsername(string $username)
{
$this->username = $username;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Retrieve table name by type
*
* @param string $type
*
* @return string
*/
static public function getTable(string $type)
{
$self = new self($type);
return $self->table;
}
/**
* Retrieve user groups
*
* @param $username
*
* @return array|string
*/
public function getGroupsData($username)
{
$this->username = $username;
if ($this->type == 'fe') {
return $this->getFEUserGroups();
} else {
return $this->getBEUserGroups();
}
}
/**
* Retrieve valid backend groups
*
* @return array
*/
public function getBEUserGroups(): array
{
// Pre-set array of groups
$arrGroups = [];
// Read and load groups
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->select('uid', 'title')
->from($this->table)
->where('deleted = 0 AND hidden = 0');
$result = $queryBuilder->execute()->fetchAll();
foreach ($result as $row) {
$arrGroups[] = $row;$arrGroups[] = $row;
}
return $arrGroups;
}
/**
* Retrieve valid frontend groups or create new ones, if none present
*
* @return string
*/
protected function getFEUserGroups()
{
$eduPersonAffiliation = $this->getServerVar($this->conf['remoteUserGroups']);
// Prepare groups
if (empty($eduPersonAffiliation)) {
$eduPersonAffiliation = 'member';
}
if (!empty($eduPersonAffiliation)) {
$affiliation = explode(';', $eduPersonAffiliation);
array_walk($affiliation, function (&$v, $k) {
$v = preg_replace("/@.*/", "", $v);
});
}
// Get local groups to keep after sync
$feGroups = $this->getFEUserLocalGroups();
// Check if new groups here
if (!empty($affiliation)) {
// Insert the affiliations in fe_groups if they are not there.
foreach ($affiliation as $title) {
// Fetch existing
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->select('uid', 'title', 'tx_luauth_synced_group')
->from($this->table)
->where("deleted = 0 AND pid = ".$this->conf['extraStoragePid']." AND title = '$title'");
$row = $queryBuilder->execute()->fetch();
// Check if any found
if ($row) {
$feGroups[] = $row['uid'];
// Update synced status if not set yet
if (!$row['tx_luauth_synced_group']) {
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->update($this->table)
->where('uid', $row['uid'])
->set('tx_luauth_synced_group', 1)
->execute();
}
} else {
$group = [
'title' => $title,
'pid' => $this->conf['extraStoragePid'],
'tx_luauth_synced_group' => 1,
];
// Insert new one if none found
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$feGroups[] = $queryBuilder->insert($this->table)->values($group)->execute();
}
}
}
return implode(',', $feGroups);
}
/**
* Retrieve local groups
*
* @return array
*/
private function getFEUserLocalGroups()
{
$feGroups = [];
// Select usergroups
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->select('usergroup')
->from(Users::getTable('fe'))
->where("deleted = 0 AND disable=0 AND pid = ".$this->conf['extraStoragePid']." AND username = '{$this->username}'");
$row = $queryBuilder->execute()->fetch();
if ($row) {
$currentGroups = $row['usergroup'];
}
//$currentGroups is empty when user is not yet created. Prevent next sql from being syntactically incorrect by
//assigning 0 in such case
if (empty($currentGroups)) {
$currentGroups = 0;
}
// Select usergroups
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->select('uid')
->from(Groups::getTable('fe'))
->where("deleted = 0 AND pid = ".$this->conf['extraStoragePid']." AND uid IN ($currentGroups) AND TSconfig LIKE 'group.type%=%local'");
$result = $queryBuilder->execute()->fetchAll();
foreach ($result as $row) {
$feGroups[] = $row['uid'];
}
// Select groups without sync mention
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->select('usergroup')
->from(Users::getTable('fe'))
->where("username = '{$this->username}'");
$row = $queryBuilder->execute()->fetch();
if ($row['usergroup']) {
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->select('*')
->from(Groups::getTable('fe'))
->where("uid IN ({$row['usergroup']}) AND (tx_luauth_synced_group IS NULL OR tx_luauth_synced_group <> 1)");
$result = $queryBuilder->execute()->fetchAll();
foreach ($result as $row2) {
$feGroups[] = $row2['uid'];
}
}
return $feGroups;
}
/**
* Retrieve server variables
*
* @param $key
* @param string $prefix
*
* @return null
*/
private function getServerVar($key, $prefix = 'REDIRECT2_')
{
if (isset($_SERVER[$key])) {
return $_SERVER[$key];
} else if (isset($_SERVER[$prefix.$key])) {
return $_SERVER[$prefix.$key];
} else {
foreach ($_SERVER as $k => $v) {
if ($key == str_replace($prefix, '', $k)) {
return $v;
}
}
}
return null;
}
}
<?php
namespace Lu\LuAuth\Models;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class Sessions.
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.2
* @since 06.06.2018
*
* @package Lu\LuAuth\Models
*/
class Sessions
{
/**
* Table name
*
* @var string
*/
protected $table = 'be_sessions';
/**
* Set request type
*
* @var string
*/
protected $type = 'be';
/**
* Users constructor. Set initial usage.
*
* @param string $type 'be' or 'fe'
*/
public function __construct(string $type)
{
// Set config value
$this->conf = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)->get('lu_auth');
// Set used group
if ($type == 'fe') {
$this->table = 'fe_sessions';
} else {
$this->table = 'be_sessions';
}
// Set used group
$this->type = $type == 'fe' ? 'fe' : 'be';
}
/**
* Destroy user session
*/
public function destroy()
{
// Delete user session
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->delete($this->table)->where(
$queryBuilder->expr()->eq('ses_id', $queryBuilder->createNamedParameter($_COOKIE['fe_typo_user']))
);
$queryBuilder->execute();
// Update cookie
setcookie('fe_typo_user', false, time() - 3600, '/', null, null, null);
}
}
<?php
namespace Lu\LuAuth\Models;
use Lu\LuApi\Generators\Randomizer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class Users/ Makes requests for users.
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0
* @since 28.05.2018
*
* @package Lu\LuAuth\Models
*/
class Users
{
/**
* Extension configuration
*
* @array
*/
public $conf;
/**
* Table name
*
* @var string
*/
protected $table;
/**
* Set request type
*
* @var string
*/
protected $type = 'be';
/**
* Username
*
* @var string
*/
private $username;
/**
* Users constructor. Set initial usage.
*
* @param string $type 'be' or 'fe'
*/
public function __construct(string $type)
{
// Set config value
$this->conf = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)->get('lu_auth');
// Set used group
if ($type == 'fe') {
$this->table = 'fe_users';
} else {
$this->table = 'be_users';
}
// Set used group
$this->type = $type == 'fe' ? 'fe' : 'be';
}
/**
* Retrieve table name by type
*
* @param string $type
*
* @return string
*/
static public function getTable(string $type)
{
$self = new self($type);
return $self->table;
}
/**
* Retrieve valid username
*
* @param $username
*
* @return array
*/
public function getUserData($username, string $options = NULL): array
{
// Check if username given
if (empty($username)) {
return [];
} else {
$this->username = $username;
}
// Request user data from DB
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->select('*')
->from($this->table)
->where('disable = 0 AND deleted = 0')
->andWhere($queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($this->username)));
$user = $queryBuilder->execute()->fetch();
// Check if options
if (empty($user) && $options == 'activate') {
// Update user and refetch, if something updated
$result = $this->activateUser();
if ($result) {
$user = self::getUserData($username);
}
}
// Return user
return empty($user) ? [] : $user;
}
/**
* Activate user
*
* @return array|bool
*/
public function activateUser()
{
// Check for where parameters and username
if (empty($this->username)) {
return [];
}
// Update user data in DB
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->update($this->table)
->where($queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($this->username)))
->set('disable', '0')
->set('deleted', '0');
$result = $queryBuilder->execute();
// Return affected rows, if any
return $result;
}
/**
* Update user data
*
* @param $user
*
* @return array
*/
public function updateUser($user)
{
// Pre-set used models
$GroupsModel = new Groups($this->type);
// @TODO: Log event here
//$this->writelog(255,3,3,2, "Importing user %s!", array($this->remoteUser));
// Get groups string
$arrGroups = $GroupsModel->getGroupsData($user['username']);
$user = [
'tstamp' => time(),
'email' => $user['email'] ?? '',
'username' => $user['username'],
'name' => $user['name'] ?? '',
'first_name' => $user['first_name'] ?? '',
'last_name' => $user['last_name'] ?? '',
'telephone' => $user['telephone'] ?? '',
'address' => $user['address'] ?? '',
'usergroup' => $arrGroups,
];
// Update database
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->update($this->table);
$queryBuilder->where($queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($this->username)));
// Set user fields. There's got to be a better way >.<
foreach ($user as $key => $value) {
$queryBuilder->set($key, $value);
}
$queryBuilder->execute();
// Request new user and return
return $this->getUserData($user['username']);
}
/**
* Create new user and return its data
*
* @param array $user
*
* @return array
*/
public function insertUser($user = [])
{
// Pre-set used models
$ApiRand = new Randomizer();
$GroupsModel = new Groups($this->type);
// @TODO: Log event here
//$this->writelog(255,3,3,2, "Importing user %s!", array($this->remoteUser));
// Get password string
$password = $ApiRand->getRandomString(32);
$password = json_decode($password);
// Get groups string
$arrGroups = $GroupsModel->getGroupsData($user['username']);
$user = [
'crdate' => time(),
'tstamp' => time(),
'pid' => $this->conf['extraStoragePid'],
'password' => $password->data,
'email' => $user['email'],
'username' => $user['username'],
'name' => $user['name'],
'first_name' => $user['first_name'],
'last_name' => $user['last_name'],
'telephone' => $user['telephone'],
'address' => (string)$user['address'],
'usergroup' => $arrGroups,
];
// Remove empty values
foreach($user as $k=>$item) {
if (empty($item)) {
unset($user[$k]);
}
}
// Save to database
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->insert($this->table)->values($user)->execute();
// Request new user and return
return $this->getUserData($user['username']);
}
}
<?php
namespace Lu\LuAuth\Services;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Lu\LuApi\Authentication\LuisLibrary;
use Lu\LuAuth\Models\Users;
use Lu\LDAPAtuhorization\LDAP;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Authentication\AuthenticationService;