Exemplo n.º 1
0
 public function testIndexAction()
 {
     $client = $this->createAuthenticatedClient();
     $client->request('GET', '/admin/');
     // required because test user does not exist otherwise
     $userSetting = new UserSetting();
     $userSetting->setKey('test');
     $userSetting->setValue(json_encode(['key' => 'value']));
     $userSetting->setUser($this->getTestUser());
     $this->em->persist($userSetting);
     $this->em->flush();
     $client->request('GET', '/admin/');
     $this->assertContains('"settings":{"test":{"key":"value"}}', $client->getResponse()->getContent());
 }
Exemplo n.º 2
0
 /**
  * Takes a key, value pair and stores it as settings for the user.
  *
  * @param Request $request
  *
  * @return Response
  */
 public function putSettingsAction(Request $request)
 {
     $key = $request->get('key');
     $value = $request->get('value');
     try {
         if (!$key) {
             throw new MissingArgumentException(static::$entityNameUserSetting, 'key');
         }
         if (!$value) {
             throw new MissingArgumentException(static::$entityNameUserSetting, 'value');
         }
         $user = $this->tokenStorage->getToken()->getUser();
         // encode before persist
         $data = json_encode($value);
         // get setting
         // TODO: move this logic into own service (UserSettingManager?)
         $setting = $this->userSettingRepository->findOneBy(['user' => $user, 'key' => $key]);
         // or create new one
         if (!$setting) {
             $setting = new UserSetting();
             $setting->setKey($key);
             $setting->setUser($user);
             $this->objectManager->persist($setting);
         }
         // persist setting
         $setting->setValue($data);
         $this->objectManager->flush();
         //create view
         $view = View::create($setting, 200);
     } catch (RestException $exc) {
         $view = View::create($exc->toArray(), 400);
     }
     return $this->viewHandler->handle($view);
 }
Exemplo n.º 3
0
 /**
  * persists the user data to the database.
  *
  * @param $key
  * @param $value
  */
 public function setUserSetting($key, $value)
 {
     $em = $this->registry->getManager();
     $user = $this->getUser();
     // encode before persist
     $data = json_encode($value);
     // get setting
     /** @var UserSetting $setting */
     $setting = $this->registry->getRepository('SuluSecurityBundle:UserSetting')->findOneBy(['user' => $user, 'key' => $key]);
     // or create new one
     if (!$setting) {
         $setting = new UserSetting();
         $setting->setKey($key);
         $setting->setUser($user);
         $em->persist($setting);
     }
     // persist setting
     $setting->setValue($data);
     $em->flush($setting);
 }