Commit 8c9b01d7 authored by Stefan Busemann's avatar Stefan Busemann
Browse files

[WIP] Add hook code

parent 04af627b
Pipeline #1531 passed with stages
in 4 minutes and 6 seconds
<?php
namespace T3o\T3omy\Hooks;
/***************************************************************
* Copyright notice
*
......@@ -26,41 +27,119 @@ namespace T3o\T3omy\Hooks;
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use In2code\Femanager\Domain\Model\User;
use T3o\T3oLdap\Connectors\Ldap;
use T3o\T3oLdap\Utility\UserCreateUpdateDelete;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use T3o\T3oSlack\Controller\SlackUserController;
/**
* Hook Functions for \
*/
class FeManagerHooks
{
/**
* @var UserCreateUpdateDelete
*/
protected $ldap;
protected $ldapUserCreateUpdateDelete;
/**
* DataHandlerHook constructor
*/
public function __construct()
{
// @todo make instance of ldap
#$this->ldap = new Tx_T3oLdap_Utility_UserCreateUpdateDelete();
$this->ldapUserCreateUpdateDelete = new UserCreateUpdateDelete();
}
public function newUserNotification()
{
// this event happens, when a user registered himself and no autoapprovement got active
die ('newUserNotification');
// @todo slack message notification
$this->sendConfirmationMessage($user);
$this->sendSlackBotMessage(
'New User Registration',
sprintf(
'new user registered on typo3.org with username: *%s* email: *%s* name: *%s* and IP located in: %s',
$user->getUsername(),
$user->getEmail(),
$user->getName(),
$mapsLink
),
'notice'
);
}
public function newUserAutoApprovement()
{
// this event happens, when a user registered himself and an autoapprovement got active
die('AutoApprovementDone');
// @todo slack message notification
$this->sendSlackBotMessage(
'New user auto approvement',
sprintf(
'new user registered on typo3.org with username: *%s* email: *%s* name: *%s* and IP located in: %s was auto approved',
$user->getUsername(),
$user->getEmail(),
$user->getName(),
$mapsLink
),
'info'
);
}
public function newUserAdminApprovement()
{
#AutoApprovementRefused
die('AdminApprovementDone');
$this->sendSlackBotMessage(
'User approved',
sprintf(
'the user *%s* with email *%s* has been approved by *%s*',
$user->getUsername(),
$user->getEmail(),
$GLOBALS['BE_USER']->user['username']
),
'ok'
);
}
public function newUserAdminDecline()
{
#AutoApprovementRefused
die('AdminApprovementRefused');
$this->sendSlackBotMessage(
'User declined',
sprintf(
'the user *%s* with email *%s* has been declined by *%s*',
$username,
$user->getEmail(),
$GLOBALS['BE_USER']->user['username']
),
'danger'
);
}
/**
* Use DataHandler "afterAllOperations" hook to update or create FE Users
* in LDAP.
*
* @param $user $user
* @param \In2code\Femanager\Domain\Model\User $user
* @return void
*/
public function createUser(User $user)
{
$result = $this->ldap->updateUser($user->getUid(), $this->userObjectToArray($user));
$result = $this->ldapUserCreateUpdateDelete->updateUser($user->getUid(), $user->toArray());
// TODO: Create log entry if user has been successfully added or throw an exception othwerwise
// TODO: Create log entry if user has been successfully added or throw an exception otherwise
if ($result === true) {
} else {
}
}
......@@ -68,12 +147,12 @@ class FeManagerHooks
* Use DataHandler "afterAllOperations" hook to update or create FE Users
* in LDAP.
*
* @param $user $user
* @param \In2code\Femanager\Domain\Model\User $user
* @return void
*/
public function updateUser(User $user)
{
$result = $this->ldap->updateUser($user->getUid(), $this->userObjectToArray($user));
$result = $this->ldapUserCreateUpdateDelete->updateUser($user->getUid(), $user->toArray());
// TODO: Create log entry if user has been successfully updated or throw an exception othwerwise
if ($result === true) {
......@@ -85,16 +164,16 @@ class FeManagerHooks
* Use DataHandler "afterAllOperations" hook to update or create FE Users
* in LDAP.
*
* @param $user $user
* @param \In2code\Femanager\Domain\Model\User $user
* @return void
*/
public function deleteUser(User $user)
{
// TODO: Add this function in Tx_T3oLdap_Utility_UserCreateUpdateDelete
$userData = $this->userObjectToArray($user);
/** @var Tx_T3oLdap_Connectors_Ldap $ldap */
$ldap = GeneralUtility::makeInstance(Tx_T3oLdap_Connectors_Ldap::class);
$userData = $user->toArray();
$ldap = GeneralUtility::makeInstance(Ldap::class);
if ($ldap->userExists($userData['username'])) {
......@@ -107,38 +186,46 @@ class FeManagerHooks
}
}
/**
* // TODO: Options: Leave this here, move it into a service or move it to t3o_ldap
*
* @param User $user
* @return array
*/
protected function userObjectToArray(User $user)
{
$userArray = [];
$userArray['username'] = $user->getUsername();
// TODO: Add all needed fields for LDAP
return $userArray;
}
/**
* @param $_funcRef
* @param $_params
* @param bool
*/
public function writePassword($_funcRef, $_params, $felogin=false)
public function writePassword($_funcRef, $_params, $felogin = false)
{
$feUserUid = $_funcRef['user']['uid'];
$userData = $_funcRef['user'];
// set new password
$userData['password']= $_funcRef['newPasswordUnencrypted'];
$userData['password'] = $_funcRef['newPasswordUnencrypted'];
$createIfNotExists = false;
// @todo add error handling
$result = $this->ldap->updateUser($feUserUid, $userData, $createIfNotExists);
$result = $this->ldapUserCreateUpdateDelete->updateUser($feUserUid, $userData, $createIfNotExists);
}
/**
* @param string $title
* @param string $text
* @param string $color
*/
protected function sendSlackBotMessage($title, $text, $color = 'notice')
{
$url = $this->settings['webhook']['url'];
$content = json_encode(array(
'securityToken' => $this->settings['webhook']['securityToken'],
'color' => $color,
'title' => $title,
'text' => $text
));
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_exec($curl);
curl_close($curl);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment