コード例 #1
0
 public function onRequest(Request $request)
 {
     $session = $request->getSession();
     list($id, $role) = $session->get('user', array(null, 'ROLE_ANONYMOUS'));
     if (null === $id && $request->cookies->has(Remember::REMEMBER_ME)) {
         if ($this->remember->check($request->cookies->get(Remember::REMEMBER_ME))) {
             list($id, $role) = $this->remember->getIt();
             $session->set('user', array($id, $role));
         }
     }
     $this->provider->setRole($role);
     if (!$this->provider->isAllowed($request->getPathInfo())) {
         throw new Exception\AccessDeniedException("Access denied to " . $request->getPathInfo());
     }
     if (null !== $id) {
         // Ban check
         $clientIp = $request->getClientIp();
         $ban = Ban::findActive($id, $clientIp);
         if (!empty($ban)) {
             throw new BannedException($ban[0], Response::HTTP_FORBIDDEN);
         }
         // User loading.
         $user = User::find($id);
         if (null !== $user) {
             $user->ip = $clientIp;
             $user->save();
             $this->provider->setUser($user);
             $this->provider->setAuthenticated(true);
         }
     }
 }
コード例 #2
0
ファイル: Bans.php プロジェクト: elfchat/elfchat
 /**
  * @Route("/users", name="query_users")
  */
 public function users()
 {
     $users = User::queryNames($this->request->get('query'));
     $users = array_map(function (User $user) {
         return $user->export();
     }, $users);
     return $this->app->json($users);
 }
コード例 #3
0
ファイル: AbstractServer.php プロジェクト: elfchat/elfchat
 protected function privateMessage(User $user, $forId, $data)
 {
     $message = new Message();
     $message->user = $user;
     $message->for = User::find($forId);
     $message->datetime = new \DateTime();
     $message->data = $data;
     $message->save();
     $this->sendToUser($forId, Protocol::message($message));
     return $message;
 }
コード例 #4
0
ファイル: Users.php プロジェクト: elfchat/elfchat
 /**
  * @Route("/delete/{id}", name="admin_users_delete")
  */
 public function remove($id)
 {
     $user = User::find($id);
     if (!$user) {
         throw new NotFoundHttpException($this->app->trans('User not found'));
     }
     $form = $this->app->form()->add('delete', 'submit')->getForm();
     $form->handleRequest($this->request);
     if ($form->isValid()) {
         if ($form->get('delete')->isClicked()) {
             $this->app->entityManager()->remove($user);
             $this->app->entityManager()->flush();
             $message = $this->app->trans('User "%name%" was deleted.', array('%name%' => $user->name));
             $this->app->session()->getFlashBag()->add('success', $message);
             return $this->app->redirect($this->app->url('admin_users'));
         }
     }
     return $this->render('admin/users/delete.twig', array('user' => $user, 'form' => $form->createView()));
 }
コード例 #5
0
ファイル: Login.php プロジェクト: elfchat/elfchat
 /**
  * @Route("/login", name="login")
  */
 public function check()
 {
     $session = $this->app->session();
     $em = $this->app->entityManager();
     $form = $this->app->form()->add('username')->add('password', 'password')->add('remember_me', 'checkbox', array('required' => false))->getForm();
     $form->handleRequest($this->request);
     if ($form->isValid()) {
         $data = $form->getData();
         try {
             $user = User::findOneByName($data['username']);
         } catch (NoResultException $e) {
             $user = null;
         }
         if (null !== $user) {
             if (password_verify($data['password'], $user->password)) {
                 $session->set('user', $saved = array($user->id, $user->role));
                 $response = $this->app->redirect($this->app->url('chat'));
                 if ($data['remember_me']) {
                     $remember = $this->app['security.remember']->encode($saved);
                     $response->headers->setCookie(new Cookie(Remember::REMEMBER_ME, $remember, time() + 3600 * 24 * 7));
                 }
                 return $response;
             }
         }
         $error = $this->app->trans('Bad credentials');
     }
     $guestForm = $this->app->form()->add('guestname', 'text', array('constraints' => new UserName()))->getForm();
     $guestForm->handleRequest($this->request);
     if ($guestForm->isValid()) {
         $data = $guestForm->getData();
         $guest = new GuestUser();
         $guest->name = $data['guestname'];
         $em->persist($guest);
         $em->flush($guest);
         $session->set('user', array($guest->id, $guest->role));
         return $this->app->redirect($this->app->url('chat'));
     }
     $response = $this->render('login/login.twig', array('error' => isset($error) ? $error : null, 'form' => $form->createView(), 'guestForm' => $guestForm->createView()));
     return $response;
 }
コード例 #6
0
ファイル: WebSocketServer.php プロジェクト: elfchat/elfchat
 /**
  * @param ConnectionInterface $conn
  */
 public function onOpen(ConnectionInterface $conn)
 {
     $userData = $conn->Session->get('user');
     if (count($userData) == 2 && is_int($userData[0])) {
         list($userId, $userRole) = $userData;
         $user = User::find($userId);
         if (null === $user) {
             $conn->close();
             return;
         }
         $this->em->refresh($user);
         $conn->user = $user;
         $this->send(Protocol::userJoin($user));
         $this->clients[$user->id] = $conn;
         $users = array();
         foreach ($this->clients as $conn) {
             $users[] = $conn->user->export();
         }
         $this->sendToUser($user->id, Protocol::data(Protocol::SYNCHRONIZE, $users));
     } else {
         $conn->close();
     }
 }
コード例 #7
0
ファイル: Protocol.php プロジェクト: elfchat/elfchat
 public static function userUpdate(User $user)
 {
     return self::data(self::USER_UPDATE, $user->export());
 }
コード例 #8
0
ファイル: GuestUser.php プロジェクト: elfchat/elfchat
 public function __construct()
 {
     parent::__construct();
     $this->role = 'ROLE_GUEST';
 }
コード例 #9
0
ファイル: AjaxServer.php プロジェクト: elfchat/elfchat
 public function kill($userId)
 {
     $this->send(Protocol::userLeave(User::find($userId)));
 }