CurrentUser.php 13.1 KB
Newer Older
Open Source Developer's avatar
Open Source Developer committed
1
<?php
Open Source Developer's avatar
Open Source Developer committed
2

Open Source Developer's avatar
Open Source Developer committed
3 4 5 6 7 8
namespace Drupal\epal\Controller;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;
Open Source Developer's avatar
Open Source Developer committed
9 10
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
11 12
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
Open Source Developer's avatar
Open Source Developer committed
13

14 15 16
class CurrentUser extends ControllerBase
{
    protected $entityTypeManager;
17 18
    protected $logger;
    protected $connection;
Open Source Developer's avatar
Open Source Developer committed
19

20 21 22 23 24
    public function __construct(
        EntityTypeManagerInterface $entityTypeManager,
        Connection $connection,
        LoggerChannelFactoryInterface $loggerChannel
        )
25 26
    {
        $this->entityTypeManager = $entityTypeManager;
27 28
        $this->connection = $connection;
        $this->logger = $loggerChannel->get('epal');
29
    }
Open Source Developer's avatar
Open Source Developer committed
30

31 32 33
    public static function create(ContainerInterface $container)
    {
        return new static(
34 35 36
            $container->get('entity_type.manager'),
            $container->get('database'),
            $container->get('logger.factory')
37
        );
38
    }
Open Source Developer's avatar
Open Source Developer committed
39

40
    public function getLoginInfo(Request $request)
Open Source Developer's avatar
Open Source Developer committed
41
    {
42

43
        $authToken = $request->headers->get('PHP_AUTH_USER');
44
//        echo("authtoken in controller=" . $authToken);
45 46 47 48 49 50 51 52
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if (!$user) {
            return $this->respondWithStatus([
                    'message' => t("User not found"),
                ], Response::HTTP_FORBIDDEN);
        }

53 54 55 56 57 58 59 60
        $epalConfigs = $this->entityTypeManager->getStorage('epal_config')->loadByProperties(array('name' => 'epal_config'));
        $epalConfig = reset($epalConfigs);
        if (!$epalConfig) {
            return $this->respondWithStatus([
                    'message' => t("Configuration not found"),
                ], Response::HTTP_FORBIDDEN);
        }

61 62
        $userRoles = $user->getRoles();
        foreach ($userRoles as $userRole) {
Open Source Developer's avatar
trans  
Open Source Developer committed
63
            if (($userRole === 'epal') || ($userRole === 'regioneduadmin') || ($userRole === 'eduadmin')) {
64
                return $this->respondWithStatus([
65
                        'cu_name' => $user->mail->value,
66 67 68 69
                        'cu_surname' => '',
                        'cu_fathername' => '',
                        'cu_mothername' => '',
                        'cu_email' => '',
70 71 72 73 74 75 76
                        'minedu_username' => '',
                        'minedu_userpassword' => '',
                        'lock_capacity' => $epalConfig->lock_school_capacity->value,
                        'lock_students' => $epalConfig->lock_school_students_view->value,
                        'lock_application' => $epalConfig->lock_application->value,
                        'disclaimer_checked' => "0",
                        'title' => $user->init->value
77 78 79
                    ], Response::HTTP_OK);
            } else if ($userRole === 'applicant') {
                break;
80
            }
81 82

        }
83 84 85 86

        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
87 88 89 90 91
            $userName = $epalUser->name->value;
            $userSurname = $epalUser->surname->value;
            $userFathername = $epalUser->fathername->value;
            $userMothername = $epalUser->mothername->value;
            $userEmail = $user->mail->value;
92
            return $this->respondWithStatus([
93 94 95 96 97 98 99 100 101 102 103 104
                    'cu_name' => mb_substr($epalUser->name->value,0,4,'UTF-8') !== "####" ? $epalUser->name->value : '',
                    'cu_surname' => mb_substr($epalUser->surname->value,0,4,'UTF-8') !== "####" ? $epalUser->surname->value : '',
                    'cu_fathername' => mb_substr($epalUser->fathername->value,0,4,'UTF-8') !== "####" ? $epalUser->fathername->value : '',
                    'cu_mothername' => mb_substr($epalUser->mothername->value,0,4,'UTF-8') !== "####" ? $epalUser->mothername->value : '',
                    'cu_email' => mb_substr($user->mail->value,0,4,'UTF-8') !== "####" ? $user->mail->value : '',
                    'minedu_username' => '',
                    'minedu_userpassword' => '',
                    'lock_capacity' => $epalConfig->lock_school_capacity->value,
                    'lock_students' => $epalConfig->lock_school_students_view->value,
                    'lock_application' => $epalConfig->lock_application->value,
                    'disclaimer_checked' => "0",
                    'verificationCodeVerified' => $epalUser->verificationcodeverified->value,
105 106 107 108 109 110
                ], Response::HTTP_OK);
        } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
Open Source Developer committed
111 112
    }

113 114 115
    public function getEpalUserData(Request $request)
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
            $user = $this->entityTypeManager->getStorage('user')->load($epalUser->user_id->target_id);
            if ($user) {
                $userName = $epalUser->name->value;
                $userSurname = $epalUser->surname->value;
                $userFathername = $epalUser->fathername->value;
                $userMothername = $epalUser->mothername->value;
                $userEmail = $user->mail->value;
                return $this->respondWithStatus([
                    'userName' => mb_substr($epalUser->name->value,0,4,'UTF-8') !== "####" ? $epalUser->name->value : '',
                    'userSurname' => mb_substr($epalUser->surname->value,0,4,'UTF-8') !== "####" ? $epalUser->surname->value : '',
                    'userFathername' => mb_substr($epalUser->fathername->value,0,4,'UTF-8') !== "####" ? $epalUser->fathername->value : '',
                    'userMothername' => mb_substr($epalUser->mothername->value,0,4,'UTF-8') !== "####" ? $epalUser->mothername->value : '',
                    'userEmail' => mb_substr($user->mail->value,0,4,'UTF-8') !== "####" ? $user->mail->value : '',
133
                    'verificationCodeVerified' => $epalUser->verificationcodeverified->value,
134 135 136 137 138 139
                ], Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t("user not found"),
                ], Response::HTTP_INTERNAL_SERVER_ERROR);
            }
Open Source Developer's avatar
Open Source Developer committed
140

141 142 143 144 145 146 147
        } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
    }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    public function sendVerificationCode(Request $request)
    {

        if (!$request->isMethod('POST')) {
			return $this->respondWithStatus([
					"message" => t("Method Not Allowed")
				], Response::HTTP_METHOD_NOT_ALLOWED);
    	}
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $trx = $this->connection->startTransaction();
        try {
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
            $user = $this->entityTypeManager->getStorage('user')->load($epalUser->user_id->target_id);
            if ($user) {
                $postData = null;
                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    $verificationCode = uniqid();
                    $epalUser->set('verificationcode', $verificationCode);
                    $epalUser->set('verificationcodeverified', FALSE);
                    $epalUser->save();
                    $user->set('mail', $postData->userEmail);
                    $user->save();
                    $this->sendEmailWithVerificationCode($postData->userEmail, $verificationCode, $user);
                    return $this->respondWithStatus([
                        'userEmail' => $postData->userEmail,
                        'verCode' => $verificationCode,
                    ], Response::HTTP_OK);
                }
                else {
                    return $this->respondWithStatus([
                        'message' => t("post with no data"),
                    ], Response::HTTP_BAD_REQUEST);
                }

            } else {
                return $this->respondWithStatus([
                    'message' => t("user not found"),
                ], Response::HTTP_INTERNAL_SERVER_ERROR);
            }

        } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
        } catch (\Exception $ee) {
            $this->logger->warning($ee->getMessage());
            $trx->rollback();
            return false;
        }

    }

    private function sendEmailWithVerificationCode($email, $vc, $user) {
        $mailManager = \Drupal::service('plugin.manager.mail');

        $module = 'epal';
        $key = 'send_verification_code';
        $to = $email;
211
        $params['message'] = 'Κωδικός επαλήθευσης=' . $vc;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        $langcode = $user->getPreferredLangcode();
        $send = true;

        $mail_sent = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

        if ($mail_sent) {
            $this->logger->info("Mail Sent successfully.");
        }
        else {
            $this->logger->info("There is error in sending mail.");
        }
        return;
    }


    public function verifyVerificationCode(Request $request)
    {

        if (!$request->isMethod('POST')) {
			return $this->respondWithStatus([
					"message" => t("Method Not Allowed")
				], Response::HTTP_METHOD_NOT_ALLOWED);
    	}
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {

            $user = $this->entityTypeManager->getStorage('user')->load($epalUser->user_id->target_id);
            if ($user) {
                $postData = null;
                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    if ($epalUser->verificationcode->value !== $postData->verificationCode) {
                        return $this->respondWithStatus([
                            'userEmail' => $user->mail->value,
                            'verificationCodeVerified' => false
                        ], Response::HTTP_OK);
                    } else {
                        $epalUser->set('verificationcodeverified', true);
                        $epalUser->save();
                        return $this->respondWithStatus([
                            'userEmail' => $user->mail->value,
                            'verificationCodeVerified' => true
                        ], Response::HTTP_OK);
                    }
                }
            } else {
                return $this->respondWithStatus([
                    'message' => t("user not found"),
                ], Response::HTTP_INTERNAL_SERVER_ERROR);
            }

        } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
    }
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    public function saveUserProfile(Request $request)
    {

        if (!$request->isMethod('POST')) {
			return $this->respondWithStatus([
					"message" => t("Method Not Allowed")
				], Response::HTTP_METHOD_NOT_ALLOWED);
    	}
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
            $postData = null;
            if ($content = $request->getContent()) {
                $postData = json_decode($content);
                $epalUser->set('name', $postData->userProfile->userName);
                $epalUser->set('surname', $postData->userProfile->userSurname);
                $epalUser->set('mothername', $postData->userProfile->userMothername);
                $epalUser->set('fathername', $postData->userProfile->userFathername);
                $epalUser->save();
                return $this->respondWithStatus([
                    'message' => t("profile saved"),
                ], Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t("post with no data"),
                ], Response::HTTP_BAD_REQUEST);
            }

        } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
    }

310 311 312 313 314
    private function respondWithStatus($arr, $s) {
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
        return $res;
    }
Open Source Developer's avatar
Open Source Developer committed
315
}