]> Raphaël G. Git Repositories - userbundle/blobdiff - Checker/UserChecker.php
Add user checker
[userbundle] / Checker / UserChecker.php
diff --git a/Checker/UserChecker.php b/Checker/UserChecker.php
new file mode 100644 (file)
index 0000000..1240c8a
--- /dev/null
@@ -0,0 +1,48 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys UserBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\UserBundle\Checker;
+
+use Symfony\Component\Security\Core\User\UserChecker as BaseUserChecker;
+use Symfony\Component\Security\Core\Exception\DisabledException;
+use Symfony\Component\Security\Core\User\UserInterface;
+
+use Rapsys\UserBundle\Entity\User;
+use Rapsys\UserBundle\Exception\UnactivatedException;
+
+/**
+ * {@inheritdoc}
+ */
+class UserChecker extends BaseUserChecker {
+       /**
+        * {@inheritdoc}
+        */
+       public function checkPostAuth(UserInterface $user): void {
+               //Without User instance
+               if (!$user instanceof User) {
+                       return;
+               }
+
+               //With not activated user
+        if (!$user->isActivated()) {
+            $ex = new UnactivatedException('Account is not activated');
+            $ex->setUser($user);
+            throw $ex;
+        }
+
+               //With disabled user
+        if ($user->isDisabled()) {
+            $ex = new DisabledException('Account is disabled');
+            $ex->setUser($user);
+            throw $ex;
+        }
+       }
+}