mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-10 02:14:31 +02:00
Mark SAML users as so in database and disable local password changing then.
This commit is contained in:
parent
78ec0f1ea3
commit
97c3b9002a
15 changed files with 1414 additions and 1264 deletions
|
@ -83,6 +83,10 @@ class UserSettingsController extends AbstractController
|
|||
return new RuntimeException('This controller only works only for Part-DB User objects!');
|
||||
}
|
||||
|
||||
if ($user->isSamlUser()) {
|
||||
throw new RuntimeException('You can not remove U2F keys from SAML users!');
|
||||
}
|
||||
|
||||
if (empty($user->getBackupCodes())) {
|
||||
$this->addFlash('error', 'tfa_backup.no_codes_enabled');
|
||||
|
||||
|
@ -112,6 +116,10 @@ class UserSettingsController extends AbstractController
|
|||
throw new RuntimeException('This controller only works only for Part-DB User objects!');
|
||||
}
|
||||
|
||||
if ($user->isSamlUser()) {
|
||||
throw new RuntimeException('You can not remove U2F keys from SAML users!');
|
||||
}
|
||||
|
||||
if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
|
||||
//Handle U2F key removal
|
||||
if ($request->request->has('key_id')) {
|
||||
|
@ -192,6 +200,10 @@ class UserSettingsController extends AbstractController
|
|||
return new RuntimeException('This controller only works only for Part-DB User objects!');
|
||||
}
|
||||
|
||||
if ($user->isSamlUser()) {
|
||||
throw new RuntimeException('You can not remove U2F keys from SAML users!');
|
||||
}
|
||||
|
||||
if ($this->isCsrfTokenValid('devices_reset'.$user->getId(), $request->request->get('_token'))) {
|
||||
$user->invalidateTrustedDeviceTokens();
|
||||
$entityManager->flush();
|
||||
|
@ -281,14 +293,14 @@ class UserSettingsController extends AbstractController
|
|||
])
|
||||
->add('old_password', PasswordType::class, [
|
||||
'label' => 'user.settings.pw_old.label',
|
||||
'disabled' => $this->demo_mode,
|
||||
'disabled' => $this->demo_mode || $user->isSamlUser(),
|
||||
'attr' => [
|
||||
'autocomplete' => 'current-password',
|
||||
],
|
||||
'constraints' => [new UserPassword()],
|
||||
]) //This constraint checks, if the current user pw was inputted.
|
||||
->add('new_password', RepeatedType::class, [
|
||||
'disabled' => $this->demo_mode,
|
||||
'disabled' => $this->demo_mode || $user->isSamlUser(),
|
||||
'type' => PasswordType::class,
|
||||
'first_options' => [
|
||||
'label' => 'user.settings.pw_new.label',
|
||||
|
@ -307,7 +319,10 @@ class UserSettingsController extends AbstractController
|
|||
'max' => 128,
|
||||
])],
|
||||
])
|
||||
->add('submit', SubmitType::class, ['label' => 'save'])
|
||||
->add('submit', SubmitType::class, [
|
||||
'label' => 'save',
|
||||
'disabled' => $this->demo_mode || $user->isSamlUser(),
|
||||
])
|
||||
->getForm();
|
||||
|
||||
$pw_form->handleRequest($request);
|
||||
|
@ -327,7 +342,9 @@ class UserSettingsController extends AbstractController
|
|||
}
|
||||
|
||||
//Handle 2FA things
|
||||
$google_form = $this->createForm(TFAGoogleSettingsType::class, $user);
|
||||
$google_form = $this->createForm(TFAGoogleSettingsType::class, $user, [
|
||||
'disabled' => $this->demo_mode || $user->isSamlUser(),
|
||||
]);
|
||||
$google_enabled = $user->isGoogleAuthenticatorEnabled();
|
||||
if (!$google_enabled && !$form->isSubmitted()) {
|
||||
$user->setGoogleAuthenticatorSecret($googleAuthenticator->generateSecret());
|
||||
|
@ -335,7 +352,7 @@ class UserSettingsController extends AbstractController
|
|||
}
|
||||
$google_form->handleRequest($request);
|
||||
|
||||
if (!$this->demo_mode && $google_form->isSubmitted() && $google_form->isValid()) {
|
||||
if (!$this->demo_mode && !$user->isSamlUser() && $google_form->isSubmitted() && $google_form->isValid()) {
|
||||
if (!$google_enabled) {
|
||||
//Save 2FA settings (save secrets)
|
||||
$user->setGoogleAuthenticatorSecret($google_form->get('googleAuthenticatorSecret')->getData());
|
||||
|
@ -369,7 +386,7 @@ class UserSettingsController extends AbstractController
|
|||
])->getForm();
|
||||
|
||||
$backup_form->handleRequest($request);
|
||||
if (!$this->demo_mode && $backup_form->isSubmitted() && $backup_form->isValid()) {
|
||||
if (!$this->demo_mode && !$user->isSamlUser() && $backup_form->isSubmitted() && $backup_form->isValid()) {
|
||||
$backupCodeManager->regenerateBackupCodes($user);
|
||||
$em->flush();
|
||||
$this->addFlash('success', 'user.settings.2fa.backup_codes.regenerated');
|
||||
|
|
|
@ -20,9 +20,11 @@
|
|||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Entity\UserSystem\WebauthnKey;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Jbtronics\TFAWebauthn\Services\TFAWebauthnRegistrationHelper;
|
||||
use RuntimeException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
@ -31,6 +33,13 @@ use function Symfony\Component\Translation\t;
|
|||
|
||||
class WebauthnKeyRegistrationController extends AbstractController
|
||||
{
|
||||
private bool $demo_mode;
|
||||
|
||||
public function __construct(bool $demo_mode)
|
||||
{
|
||||
$this->demo_mode = $demo_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/webauthn/register", name="webauthn_register")
|
||||
*/
|
||||
|
@ -39,6 +48,20 @@ class WebauthnKeyRegistrationController extends AbstractController
|
|||
//When user change its settings, he should be logged in fully.
|
||||
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
|
||||
|
||||
if ($this->demo_mode) {
|
||||
throw new RuntimeException('You can not do 2FA things in demo mode');
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
throw new RuntimeException('This controller only works only for Part-DB User objects!');
|
||||
}
|
||||
|
||||
if ($user->isSamlUser()) {
|
||||
throw new RuntimeException('You can not remove U2F keys from SAML users!');
|
||||
}
|
||||
|
||||
//If form was submitted, check the auth response
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$webauthnResponse = $request->request->get('_auth_code');
|
||||
|
|
|
@ -240,10 +240,16 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
|
|||
|
||||
/**
|
||||
* @var DateTime the time until the password reset token is valid
|
||||
* @ORM\Column(type="datetime", nullable=true)
|
||||
* @ORM\Column(type="datetime", nullable=true, columnDefinition="DEFAULT NULL")
|
||||
*/
|
||||
protected $pw_reset_expires;
|
||||
|
||||
/**
|
||||
* @var bool True if the user was created by a SAML provider (and therefore cannot change its password)
|
||||
* @ORM\Column(type="boolean")
|
||||
*/
|
||||
protected bool $saml_user = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
@ -863,6 +869,28 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
|
|||
$this->webauthn_keys->add($webauthnKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true, if the user was created by the SAML authentication.
|
||||
* @return bool
|
||||
*/
|
||||
public function isSamlUser(): bool
|
||||
{
|
||||
return $this->saml_user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the saml_user flag.
|
||||
* @param bool $saml_user
|
||||
* @return User
|
||||
*/
|
||||
public function setSamlUser(bool $saml_user): User
|
||||
{
|
||||
$this->saml_user = $saml_user;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function setSamlAttributes(array $attributes)
|
||||
{
|
||||
//When mail attribute exists, set it
|
||||
|
|
|
@ -65,7 +65,7 @@ class UserAdminForm extends AbstractType
|
|||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
/** @var AbstractStructuralDBElement $entity */
|
||||
/** @var User $entity */
|
||||
$entity = $options['data'];
|
||||
$is_new = null === $entity->getID();
|
||||
|
||||
|
@ -164,7 +164,7 @@ class UserAdminForm extends AbstractType
|
|||
'invalid_message' => 'password_must_match',
|
||||
'required' => false,
|
||||
'mapped' => false,
|
||||
'disabled' => !$this->security->isGranted('set_password', $entity),
|
||||
'disabled' => !$this->security->isGranted('set_password', $entity) || $entity->isSamlUser(),
|
||||
'constraints' => [new Length([
|
||||
'min' => 6,
|
||||
'max' => 128,
|
||||
|
@ -174,7 +174,7 @@ class UserAdminForm extends AbstractType
|
|||
->add('need_pw_change', CheckboxType::class, [
|
||||
'required' => false,
|
||||
'label' => 'user.edit.needs_pw_change',
|
||||
'disabled' => !$this->security->isGranted('set_password', $entity),
|
||||
'disabled' => !$this->security->isGranted('set_password', $entity) || $entity->isSamlUser(),
|
||||
])
|
||||
|
||||
->add('disabled', CheckboxType::class, [
|
||||
|
|
|
@ -57,7 +57,7 @@ class UserSettingsType extends AbstractType
|
|||
$builder
|
||||
->add('name', TextType::class, [
|
||||
'label' => 'user.username.label',
|
||||
'disabled' => !$this->security->isGranted('edit_username', $options['data']) || $this->demo_mode,
|
||||
'disabled' => !$this->security->isGranted('edit_username', $options['data']) || $this->demo_mode || $options['data']->isSamlUser(),
|
||||
])
|
||||
->add('first_name', TextType::class, [
|
||||
'required' => false,
|
||||
|
|
|
@ -45,10 +45,16 @@ class NamedDBElementRepository extends DBElementRepository
|
|||
$node->setId($entity->getID());
|
||||
$result[] = $node;
|
||||
|
||||
if ($entity instanceof User && $entity->isDisabled()) {
|
||||
//If this is an user, then add a badge when it is disabled
|
||||
$node->setIcon('fa-fw fa-treeview fa-solid fa-user-lock text-muted');
|
||||
if ($entity instanceof User) {
|
||||
if ($entity->isDisabled()) {
|
||||
//If this is an user, then add a badge when it is disabled
|
||||
$node->setIcon('fa-fw fa-treeview fa-solid fa-user-lock text-muted');
|
||||
}
|
||||
if ($entity->isSamlUser()) {
|
||||
$node->setIcon('fa-fw fa-treeview fa-solid fa-house-user text-muted');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
|
|
@ -32,6 +32,8 @@ class SamlUserFactory implements SamlUserFactoryInterface
|
|||
$user->setName($username);
|
||||
$user->setNeedPwChange(false);
|
||||
$user->setPassword('$$SAML$$');
|
||||
//This is a SAML user now!
|
||||
$user->setSamlUser(true);
|
||||
|
||||
$user->setSamlAttributes($attributes);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue