MinisterSettings.php 9.7 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 * @file
 * Contains \Drupal\query_example\Controller\QueryExampleController.
 */

7
namespace Drupal\deploysystem\Controller;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;


class MinisterSettings extends ControllerBase {

	protected $entity_query;
  protected $entityTypeManager;
  protected $logger;
  protected $connection;

	public function __construct(
		EntityTypeManagerInterface $entityTypeManager,
		QueryFactory $entity_query,
		Connection $connection,

		LoggerChannelFactoryInterface $loggerChannel)
		{
			$this->entityTypeManager = $entityTypeManager;
			$this->entity_query = $entity_query;
			$connection = Database::getConnection();
			$this->connection = $connection;
41
			$this->logger = $loggerChannel->get('deploysystem');
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    }

	public static function create(ContainerInterface $container)
    {
        return new static(
          $container->get('entity_type.manager'),
          $container->get('entity.query'),
          $container->get('database'),
          $container->get('logger.factory')
      );
    }


	public function retrieveSettings(Request $request) {

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

			 //user validation
			 $authToken = $request->headers->get('PHP_AUTH_USER');
			 $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);
			 }

			 //user role validation
			 $roles = $user->getRoles();
			 $validRole = false;
			 foreach ($roles as $role)
				 if ($role === "ministry") {
					 $validRole = true;
					 break;
				 }
			 if (!$validRole) {
					 return $this->respondWithStatus([
									 'message' => t("User Invalid Role"),
							 ], Response::HTTP_FORBIDDEN);
			 }

			 //minister settings retrieve
89 90 91 92
			 $config_storage = $this->entityTypeManager->getStorage('eggrafes_config');
	 		 $eggrafesConfigs = $config_storage->loadByProperties(array('name' => 'eggrafes_config'));
	 		 $eggrafesConfig = reset($eggrafesConfigs);
	 		 if (!$eggrafesConfig) {
93
	 				return $this->respondWithStatus([
94
	 								'message' => t("eggrafesConfig Enity not found"),
95 96 97
	 						], Response::HTTP_FORBIDDEN);
	 		 }
	 		 else {
98 99 100 101 102 103 104 105 106
	 				$capacityDisabled = $eggrafesConfig->lock_school_capacity->getString();
	 				$directorViewDisabled = $eggrafesConfig->lock_school_students_view->getString();
	 				$applicantsLoginDisabled = $eggrafesConfig->lock_application->getString();
					$applicantsAppModifyDisabled = $eggrafesConfig->lock_modify->getString();
					$applicantsAppDeleteDisabled = $eggrafesConfig->lock_delete->getString();
					$applicantsResultsDisabled = $eggrafesConfig->lock_results->getString();
					$secondPeriodEnabled = $eggrafesConfig->activate_second_period->getString();
					$dateStart = $eggrafesConfig->date_start_b_period->getString();
					$smallClassApproved = $eggrafesConfig->lock_small_classes->getString();
107
					$wsIdentEnabled = $eggrafesConfig->ws_ident->getString();
108
					$gsisIdentEnabled = $eggrafesConfig->gsis_ident->getString();
109 110 111 112 113 114 115 116 117 118 119 120 121
	 		 }
	 		 $config_storage->resetCache();

			return $this->respondWithStatus([
					//'message' => t("post successful"),
					'capacityDisabled' => $capacityDisabled,
					'directorViewDisabled' => $directorViewDisabled,
					'applicantsLoginDisabled' => $applicantsLoginDisabled,
					'applicantsAppModifyDisabled' => $applicantsAppModifyDisabled,
					'applicantsAppDeleteDisabled' => $applicantsAppDeleteDisabled,
					'applicantsResultsDisabled' => $applicantsResultsDisabled,
					'secondPeriodEnabled' => $secondPeriodEnabled,
					'dateStart' => $dateStart,
122
					'smallClassApproved' => $smallClassApproved,
123 124
					'$wsIdentEnabled' => $wsIdentEnabled,
					'$gsisIdentEnabled' => $gsisIdentEnabled
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
			], Response::HTTP_OK);

		}	//end try

		catch (\Exception $e) {
			$this->logger->warning($e->getMessage());
			return $this->respondWithStatus([
						"message" => t("An unexpected problem occured during retrieveSettings Method ")
					], Response::HTTP_INTERNAL_SERVER_ERROR);
		}



}


public function storeSettings(Request $request, $capacityDisabled, $directorViewDisabled, $applicantsLoginDisabled, $applicantsAppModifyDisabled,
142
		$applicantsAppDeleteDisabled, $applicantsResultsDisabled, $secondPeriodEnabled,
143
		$dateStart, $smallClassApproved, $ws, $gsis ) {
144 145

	try {
146
		if (!$request->isMethod('GET')) {
147 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
				return $this->respondWithStatus([
					 "message" => t("Method Not Allowed")
						], Response::HTTP_METHOD_NOT_ALLOWED);
		 }

		 //user validation
		 $authToken = $request->headers->get('PHP_AUTH_USER');
		 $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);
		 }

		 //user role validation
		 $roles = $user->getRoles();
		 $validRole = false;
		 foreach ($roles as $role)
			 if ($role === "ministry") {
				 $validRole = true;
				 break;
			 }
		 if (!$validRole) {
				 return $this->respondWithStatus([
								 'message' => t("User Invalid Role"),
						 ], Response::HTTP_FORBIDDEN);
		 }

176 177 178 179
		 $config_storage = $this->entityTypeManager->getStorage('eggrafes_config');
 		 $eggrafesConfigs = $config_storage->loadByProperties(array('name' => 'eggrafes_config'));
 		 $eggrafesConfig = reset($eggrafesConfigs);
 		 if (!$eggrafesConfig) {
180
 				return $this->respondWithStatus([
181
 								'message' => t("eggrafesConfig Enity not found"),
182 183 184
 						], Response::HTTP_FORBIDDEN);
 		 }
 		 else {
185 186 187 188 189 190 191 192 193 194

 				  $eggrafesConfig->set('lock_school_capacity', $capacityDisabled);
					$eggrafesConfig->set('lock_school_students_view', $directorViewDisabled);
					$eggrafesConfig->set('lock_application', $applicantsLoginDisabled);
					$eggrafesConfig->set('lock_modify', $applicantsAppModifyDisabled);
					$eggrafesConfig->set('lock_delete', $applicantsAppDeleteDisabled);
					$eggrafesConfig->set('lock_results', $applicantsResultsDisabled);
					$eggrafesConfig->set('activate_second_period', $secondPeriodEnabled);
					$eggrafesConfig->set('date_start_b_period', $dateStart);
					$eggrafesConfig->set('lock_small_classes', $smallClassApproved);
195 196
					$eggrafesConfig->set('ws_ident', $ws );
					$eggrafesConfig->set('gsis_ident', $gsis );
197
					$eggrafesConfig->save();
198 199 200 201 202 203 204 205 206 207 208 209 210 211
 		 }
 		 $config_storage->resetCache();


		return $this->respondWithStatus([
				//'message' => t("post successful"),
				'capacityDisabled' => $capacityDisabled,
				'directorViewDisabled' => $directorViewDisabled,
				'applicantsLoginDisabled' => $applicantsLoginDisabled,
				'applicantsAppModifyDisabled' => $applicantsAppModifyDisabled,
				'applicantsAppDeleteDisabled' => $applicantsAppDeleteDisabled,
				'applicantsResultsDisabled' => $applicantsResultsDisabled,
				'secondPeriodEnabled' => $secondPeriodEnabled,
				'dateStart' => $dateStart,
212
				'smallClassApproved' =>$smallClassApproved,
213 214
				'$wsIdentEnabled' =>$ws,
				'$gsisIdentEnabled' =>$gsis
215 216 217 218 219 220 221 222 223 224 225 226 227
		], Response::HTTP_OK);

	}	//end try

	catch (\Exception $e) {
		$this->logger->warning($e->getMessage());
		return $this->respondWithStatus([
					"message" => t("An unexpected problem occured during storeSettings Method ")
				], Response::HTTP_INTERNAL_SERVER_ERROR);
	}



228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}

public function isWSIdentEnabled(Request $request)
{
			 $authToken = $request->headers->get('PHP_AUTH_USER');
			 $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);
			 }

			 $config_storage = $this->entityTypeManager->getStorage('eggrafes_config');
			 $eggrafesConfigs = $config_storage->loadByProperties(array('name' => 'eggrafes_config'));
			 $eggrafesConfig = reset($eggrafesConfigs);
			 if (!$eggrafesConfig)
						return $this->respondWithStatus([
							 'message' => t("eggrafesConfig Enity not found"),
						], Response::HTTP_FORBIDDEN);
			 else
						return $this->respondWithStatus(array('res' => $eggrafesConfig->ws_ident->value), Response::HTTP_OK);

251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
public function isGsisIdentEnabled(Request $request)
{
			 $authToken = $request->headers->get('PHP_AUTH_USER');
			 $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);
			 }

			 $config_storage = $this->entityTypeManager->getStorage('eggrafes_config');
			 $eggrafesConfigs = $config_storage->loadByProperties(array('name' => 'eggrafes_config'));
			 $eggrafesConfig = reset($eggrafesConfigs);
			 if (!$eggrafesConfig)
						return $this->respondWithStatus([
							 'message' => t("eggrafesConfig Enity not found"),
						], Response::HTTP_FORBIDDEN);
			 else
						return $this->respondWithStatus(array('res' => $eggrafesConfig->gsis_ident->value), Response::HTTP_OK);

}

276

277
private function respondWithStatus($arr, $s) {
278 279 280 281 282 283 284 285 286
					$res = new JsonResponse($arr);
					$res->setStatusCode($s);
					return $res;
			}




}