2018-05-17 15:16:31 +02:00
< ? php
/**
* @ copyright Copyright ( c ) 2018 Arthur Schiwon < blizzz @ arthur - schiwon . de >
*
* @ author Arthur Schiwon < blizzz @ arthur - schiwon . de >
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*
*/
namespace OCA\DataRequest\Services ;
2018-05-17 22:38:32 +02:00
use OCA\DataRequest\Exceptions\HintedRuntime ;
2018-05-17 15:16:31 +02:00
use OCP\Defaults ;
use OCP\IConfig ;
use OCP\IGroupManager ;
2018-05-17 22:38:32 +02:00
use OCP\IL10N ;
2018-05-17 15:16:31 +02:00
use OCP\IUser ;
use OCP\IUserSession ;
use OCP\L10N\IFactory ;
use OCP\Mail\IEMailTemplate ;
use OCP\Mail\IMailer ;
use OCP\Util ;
class Request {
/** @var string */
protected $defaultLanguage ;
/** @var IGroupManager */
private $groupManager ;
/** @var IMailer */
private $mailer ;
/** @var IFactory */
private $l10nFactory ;
/** @var IConfig */
private $config ;
/** @var IUser */
private $requester ;
2018-05-17 22:38:32 +02:00
/** @var IL10N */
private $l ;
2018-06-14 13:01:42 +02:00
/** @var Defaults */
private $defaults ;
2018-05-17 15:16:31 +02:00
2018-06-14 13:01:42 +02:00
public function __construct (
IGroupManager $groupManager ,
IMailer $mailer ,
IFactory $l10nFactory ,
IConfig $config ,
IUserSession $userSession ,
IL10N $l ,
Defaults $defaults
) {
2018-05-17 15:16:31 +02:00
$this -> groupManager = $groupManager ;
$this -> mailer = $mailer ;
$this -> l10nFactory = $l10nFactory ;
$this -> config = $config ;
$this -> requester = $userSession -> getUser ();
2018-05-17 22:38:32 +02:00
$this -> l = $l ;
2018-06-14 13:01:42 +02:00
$this -> defaults = $defaults ;
2018-05-17 15:16:31 +02:00
}
public function sendExportRequest () {
$this -> sendRequest ( function ( IUser $r ) { return $this -> getExportTemplate ( $r ); });
}
public function sendDeleteRequest () {
$this -> sendRequest ( function ( IUser $r ) { return $this -> getDeletionTemplate ( $r ); });
}
protected function sendRequest ( callable $templateGenerator ) {
$admins = $this -> getAdmins ();
$oneMailSent = false ;
foreach ( $admins as $admin ) {
$template = $templateGenerator ( $admin );
if ( $this -> craftEmailTo ( $admin , $template ) === true ) {
$oneMailSent = true ;
}
}
if ( ! $oneMailSent ) {
2018-05-17 22:38:32 +02:00
throw new HintedRuntime (
'No mail was sent successfully' ,
$this -> l -> t ( 'No administrator could have been contacted.' )
);
2018-05-17 15:16:31 +02:00
}
}
protected function getDefaultLang () {
if ( $this -> defaultLanguage === null ) {
$this -> defaultLanguage = $this -> config -> getSystemValue ( 'default_language' , 'en' );
}
return $this -> defaultLanguage ;
}
protected function craftEmailTo ( IUser $admin , IEMailTemplate $template ) {
2018-05-30 23:21:37 +02:00
$senderAddress = Util :: getDefaultEmailAddress ( 'no-reply' );
2018-06-14 13:01:42 +02:00
$senderName = $this -> defaults -> getName ();
2018-05-17 15:16:31 +02:00
$message = $this -> mailer -> createMessage ();
2018-06-14 13:01:42 +02:00
$message -> setTo ([ $admin -> getEMailAddress () => $admin -> getDisplayName ()]);
2018-05-17 15:16:31 +02:00
$message -> setSubject ( $template -> renderSubject ());
$message -> setHtmlBody ( $template -> renderHtml ());
$message -> setPlainBody ( $template -> renderText ());
$message -> setFrom ([ $senderAddress => $senderName ]);
try {
2018-06-14 14:41:42 +02:00
$failedRecipients = $this -> mailer -> send ( $message );
if ( count ( $failedRecipients ) > 0 ) {
return false ;
}
2018-05-17 15:16:31 +02:00
} catch ( \Exception $e ) {
2018-06-14 14:41:42 +02:00
return false ;
2018-05-17 15:16:31 +02:00
}
return true ;
}
protected function getExportTemplate ( IUser $admin ) {
$l = $this -> l10nFactory -> get ( 'data_request' , $this -> config -> getUserValue ( $admin -> getUID (), 'core' , 'lang' , $this -> getDefaultLang ()));
$template = $this -> mailer -> createEMailTemplate ( 'data_request.Export' , []);
$template -> setSubject ( $l -> t ( 'Personal data export request' ));
$template -> addHeader ();
$template -> addHeading ( $l -> t ( 'Hello %s,' ,[ $admin -> getDisplayName ()]));
$template -> addBodyText ( $l -> t ( 'The user %s, identified by user id "%s", has requested an export of his personal data. Please take action accordingly.' , [ $this -> requester -> getDisplayName (), $this -> requester -> getUID ()]));
$template -> addFooter ();
return $template ;
}
protected function getDeletionTemplate ( IUser $admin ) {
$l = $this -> l10nFactory -> get ( 'data_request' , $this -> config -> getUserValue ( $admin -> getUID (), 'core' , 'lang' , $this -> getDefaultLang ()));
$template = $this -> mailer -> createEMailTemplate ( 'data_request.Deletion' , []);
$template -> setSubject ( $l -> t ( 'Account deletion request' ));
$template -> addHeader ();
$template -> addHeading ( $l -> t ( 'Hello %s,' ,[ $admin -> getDisplayName ()]));
2018-05-22 14:34:54 +02:00
$template -> addBodyText ( $l -> t ( 'The user %s, identified by user id "%s", has requested to delete the account. Please take action accordingly.' , [ $this -> requester -> getDisplayName (), $this -> requester -> getUID ()]));
2018-05-17 15:16:31 +02:00
$template -> addFooter ();
return $template ;
}
protected function getAdmins () {
$admins = $this -> groupManager -> get ( 'admin' ) -> searchUsers ( '' );
2018-06-14 14:46:20 +02:00
$admins = array_filter ( $admins , function ( IUser $admin ) {
2018-05-17 15:16:31 +02:00
return $admin -> getEMailAddress () !== null ;
});
if ( empty ( $admins )) {
2018-05-17 22:38:32 +02:00
throw new HintedRuntime (
'No admin has entered an email address' ,
$this -> l -> t ( 'No administrator has set an email address' )
);
2018-05-17 15:16:31 +02:00
}
return $admins ;
}
}