/*
Get the user group of the user asking to be logged into the admin.
From the Request.

@param  \Illuminate\Http\Request  $request
@return string
484157
public function getRequestUserGroups($request) {

        // user id of the user trying to log in
        $user_id = $this->user->where('email', '=', $this->getRequestEmail($request) )->get()->first()->id;

        // the user's user groups (actually, the records as a GROUP (model) object
        $user_groups = $this->user->find($user_id)->group;

        // create an array
        $usergroupArray = [];
        foreach ($user_groups as $user_group) {
            $usergroupArray[] = $user_group->title;
        }

        return $usergroupArray;
    }
599207
public function getSession(Request $request)
    {
        return tap($this->sessionManager->driver(), function ($session) use ($request) {
            if ($encryptedSessionId = $request->cookies->get($session->getName())) {
                $sessionId = $this->encrypter->decrypt($encryptedSessionId);

                $session->setId($sessionId);
            }
        });
    }
476120
private function mapRequestToParentGroups(Request $request)
    {
        $groupId = $this->repository->getLocationService()->loadLocation(
            $this->parseContentRequest($request)->locationCreateStruct->parentLocationId
        )->contentId;
        $group = $this->repository->getUserService()->loadUserGroup($groupId);

        return [$group];
    }
1059295
public function checkRequest($request)
    {
        $member = Member::currentUser();
        if (!$member) {
            return false;
        }

        $salt = $request->getVar('s');
        $memberSalt = $this->memberSalt($salt, $member);
        $token = $this->getToken($memberSalt);

        // Ensure tokens match
        return $token === $request->getVar('t');
    }
1335601
public function listAction(Request $request)
    {
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');

        $em = $this->getDoctrine()->getManager();
        $configuratorClassName = '';
        if ($this->container->hasParameter('kunstmaan_user_management.user_admin_list_configurator.class')) {
            $configuratorClassName = $this->container->getParameter(
                'kunstmaan_user_management.user_admin_list_configurator.class'
            );
        }

        $configurator = new $configuratorClassName($em);

        /* @var AdminList $adminList */
        $adminList = $this->container->get('kunstmaan_adminlist.factory')->createList($configurator);
        $adminList->bindRequest($request);

        return array(
            'adminlist' => $adminList,
        );
    }
1036159
public function newUserAction(Request $request)
    {
        $adminContext = $this->get('admin.context');
        $config       = $adminContext->getActiveModuleForAction('add', 'admin-users');

        /** @var AdminUser $user */
        $user = new $config['entity']();
        $user->setIsActive(true);

        $form = $this->createFormBuilder($user, [
            'action' => $this->generateUrl('admin.user.new'),
            'attr'   => ['class' => 'form-horizontal']
        ])
            ->add('login', null, ['attr' => ['data-column' => 'text']])
            ->add('password', RepeatedType::class, [
                'type'            => PasswordType::class,
                'invalid_message' => 'The password fields must match.',
                'options'         => ['attr' => ['data-column' => 'text']],
                'required'        => true,
                'first_options'   => ['label' => 'Password'],
                'second_options'  => ['label' => 'Repeat Password'],
            ])
            ->add('rights', null, ['attr' => ['style' => 'width: 400px;', 'data-column' => 'select']])
            ->getForm();

        $form->handleRequest($request);

        if ($form->isValid()) {
            if ($user->getRights()->count()) {
                /** @var PasswordEncoderInterface $encoder */
                $encoder  = $this->get('security.encoder_factory')->getEncoder($user);
                $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());

                $user
                    ->setPassword($password)
                    ->setIsActive(true);

                $m = $this->get('doctrine')->getManager();

                $m->persist($user);
                $m->flush();

                $this->addFlash('success', 'New user has been added!');

                return $this->redirectToRoute('admin.dictionary.default', ['module' => 'admin-users']);
            } else {
                $form->get('rights')->addError(new FormError('Minimum one right need to be added.'));
            }
        }

        $vars = [
            'object'       => $user,
            'moduleConfig' => $this->get('admin.context')->getActiveModuleForAction('create'),
            'form'         => $form->createView(),
        ];

        return $this->render('@YAdmin/Security/new-user.html.twig', $vars);
    }
205518
private function getUserIdFromSession(Request $request): string
    {
        return $this->session->get($request, self::USER_KEY);
    }
1321612
protected function pullUsernameFromQuery(Request $request)
    {
        $queryUsername = $request->query->get('username');

        if ($queryUsername) {
            if (strlen($queryUsername) <= Security::MAX_USERNAME_LENGTH) {
                $request->getSession()
                    ->set(Security::LAST_USERNAME, $queryUsername);
            }
        }
    }
439006
public function targetGroupAction(Request $request)
    {
        $currentTargetGroup = null;
        if ($request->headers->has($this->targetGroupHeader)) {
            $currentTargetGroup = $this->targetGroupRepository->find($request->headers->get($this->targetGroupHeader));
        }

        $targetGroup = $this->targetGroupEvaluator->evaluate(
            $currentTargetGroup ? TargetGroupRuleInterface::FREQUENCY_SESSION : TargetGroupRuleInterface::FREQUENCY_VISITOR,
            $currentTargetGroup
        );

        $response = new Response(null, 200, [
            $this->targetGroupHeader => $targetGroup ? $targetGroup->getId() : 0,
        ]);

        return $response;
    }
500679
public function postLogin(Request $request)
    {
        $credentials = $request->only([$this->username(), 'password']);

        /** @var \Illuminate\Validation\Validator $validator */
        $validator = Validator::make($credentials, [
            $this->username()   => 'required',
            'password'          => 'required',
        ]);

        if ($validator->fails()) {
            return back()->withInput()->withErrors($validator);
        }

        if ($this->guard()->attempt($credentials)) {
            return $this->sendLoginResponse($request);
        }

        return back()->withInput()->withErrors([
            $this->username() => $this->getFailedLoginMessage(),
        ]);
    }
1150612
public function loginAction($request)
    {
        $data = $request->get('data');
        $userModel = new ModelUser();
        $validUser = $userModel->validateLogin($data['email'], $data['password']);

        if (!$validUser) {
            $this->setMessage('danger', 'Not valid user / password combination');
            return $this->redirect(url('login'));
        }

        $this->session()->set('user', $userModel->record);
        $this->sessionClearMessages();
        return $this->redirect('/');
    }
348637
public function loadUserGroups(Request $request)
    {
        $restUserGroups = array();
        if ($request->query->has('id')) {
            $userGroup = $this->userService->loadUserGroup($request->query->get('id'), Language::ALL);
            $userGroupContentInfo = $userGroup->getVersionInfo()->getContentInfo();
            $userGroupMainLocation = $this->locationService->loadLocation($userGroupContentInfo->mainLocationId);
            $contentType = $this->contentTypeService->loadContentType($userGroupContentInfo->contentTypeId);

            $restUserGroups = array(
                new Values\RestUserGroup(
                    $userGroup,
                    $contentType,
                    $userGroupContentInfo,
                    $userGroupMainLocation,
                    $this->contentService->loadRelations($userGroup->getVersionInfo())
                ),
            );
        } elseif ($request->query->has('roleId')) {
            $restUserGroups = $this->loadUserGroupsAssignedToRole($request->query->get('roleId'));
        } elseif ($request->query->has('remoteId')) {
            $restUserGroups = array(
                $this->loadUserGroupByRemoteId($request),
            );
        }

        if ($this->getMediaType($request) === 'application/vnd.ez.api.usergrouplist') {
            return new Values\UserGroupList($restUserGroups, $request->getPathInfo());
        }

        return new Values\UserGroupRefList($restUserGroups, $request->getPathInfo());
    }
118315
public function requestAction(Request $request)
    {
        $form = $this
            ->createForm(new UserForgotPassType())
            ->handleRequest($request)
        ;

        $obj = $form->getData();

        $user = $this->getDoctrine()->getRepository('FOMUserBundle:User')->findOneByUsername($obj['search']);
        if ($user === null) {
            $user = $this->getDoctrine()->getRepository('FOMUserBundle:User')->findOneByEmail($obj['search']);
        }

        if (!$user) {
            $form->addError(new FormError($this->get("templating")->render(
                'FOMUserBundle:Password:request-error-nosuchuser.html.twig',
                array("search" => $obj))));
            return array('form' => $form->createView());
        } elseif ($user->getRegistrationToken()) {
            $form->addError(new FormError($this->get("templating")->render(
                'FOMUserBundle:Password:request-error-userinactive.html.twig', array("user" => $user))));
            return array(
                'user' => $user,
                'form' => $form->createView());
        }

        $this->setResetToken($user);

        return $this->redirect($this->generateUrl('fom_user_password_send'));
    }
211094
public function search(Request $request)
    {
        if (! $request->search) {
            return redirect('admin/users');
        }

        $users = $this->service->search($request->search);
        return view('admin.users.index')->with('users', $users);
    }
591195
public function fetchToken(Request $request)
    {
        foreach ($this->config->getOperators() as $name) {
            $operator = $this->getOperator($name);

            if ($operator->hasToken($request)) {
                return $operator->fetchToken($request);
            }
        }

        return null;
    }
972837
public function index(Request $request)
	{
  		if ( Shinobi::can( config('watchtower.acl.user.index', false) ) ) {
			if ( $request->has('search_value') ) {
				$value = $request->get('search_value');
				$users = $this->model::where('name', 'LIKE', '%'.$value.'%')
					->orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
				session()->flash('search_value', $value);
			} else {
				$users = $this->model::orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
				session()->forget('search_value');	
			}
			
			return view( config('watchtower.views.users.index'), compact('users') );
	 	}

	 	return view( config('watchtower.views.layouts.unauthorized'), [ 'message' => 'view user list' ]);
	}
925590
public function forUser(Request $request)
    {
        $tokens = $this->tokenRepository->forUser($request->user()->getKey());

        return $tokens->load('client')->filter(function ($token) {
            return ! $token->client->firstParty() && ! $token->revoked;
        })->values();
    }