/** * Handles user signup (registration) form show and saving */ public function signupAction() { //Create a new for instance $form = new UserSignupForm(); //Set form $this->view->form = $form; //FORM GET //If we are not posting a form, just return page with blank form if (!$this->request->isPost()) { //no form to validate return; } //FORM SAVE //Check if form is valid, otherwise return form with errors if ($form->isValid($this->request->getPost()) != false) { //Create a new user $user = new Users(); $user->setEmptyLoginState(); //Set fields $user->login = $this->request->getPost('username', 'striptags'); $user->email = $this->request->getPost('email'); $user->password = $this->security->hash($this->request->getPost('password')); //Set view form $this->view->form = $form; //Check for username and email existance to avoid conflicts $saveConflict = false; if ($this->isUsernameExists($user->name)) { //Append flash error $this->flash->error("User name already exists!"); //Append error to the username form field $this->appendFormFieldError('username', 'Such user name already exists'); //Set conflicting state $saveConflict = true; } if ($this->isEmailExists($user->email)) { //Append flash error $this->flash->error("Email already exists!"); //Append error to the email address form field $this->appendFormFieldError('email', 'Such email already registred!'); //Set conflicting state $saveConflict = true; } //Set error if we have a conflicting user name or email address if ($saveConflict) { $this->flash->error("Conflict detected"); return; } //Try to save a user into database if ($user->save()) { return $this->response->redirect(); } //Set page errors flash if we have database errors during save operation $this->flash->error($user->getMessages()); } }
/** * Creates the remember me environment settings the related cookies and generating tokens * * @param NetAssist\Models\Users $user */ public function saveSuccessLogin($user) { $user->lastLoginDate = new MongoDate(time()); $user->failedLoginAttempts = 0; $user->save(); }
/** * Get the entity related to user in the active identity * * @return \NetAssist\Models\Users */ public function getUser() { $identity = $this->session->get('auth-identity'); if (isset($identity['id'])) { $user = Users::findById(new MongoId(substr($identity['id'], 0, 24))); if ($user == false) { throw new Exception('The user does not exist'); } return $user; } return false; }
/** * POST /Graph/Positions * Save user node positions */ public function savePositionsAction() { //disable view rendering, we rendering JSON instead of HTML $this->view->setRenderLevel(View::LEVEL_NO_RENDER); //Get user identity $identity = $this->auth->getIdentity(); if (!$identity) { //If user is not logged in, send HTTP 417 return $this->response->setStatusCode(417, "User is not logged in"); } try { //get user identitiy $uid = $this->auth->getUserId(); //get MongoDate object for current time $m_now = new \MongoDate(time()); //fetch user record $user = Users::findById($uid); if ($user == false) { //user record not found, send HTTP 403 return $this->response->setStatusCode(403, "User not found"); } //fetch user saved nodes positions $u_nodes = UserNodes::findFirst(array(array("uid" => $uid))); if ($u_nodes == false) { //nothing exists yet, add new $u_nodes = new UserNodes(); $u_nodes->uid = $uid; } //update modification time $u_nodes->lastModified = $m_now; //read JSON from request data $rawBody = $this->request->getJsonRawBody(true); //iterate over expected array foreach ($rawBody as $pos) { //save node position $node_id = (int) $pos['id']; $position = new NodePosition(); //save coordinates $position->x = (double) $pos['x']; $position->y = (double) $pos['y']; $position->node_id = $node_id; $u_nodes->positions[$node_id] = $position; } //save data to the database if ($u_nodes->save() == false) { //we have an error, send http 500 return $this->sendStateResponse(false, 500, "MongoDB save failure"); } } catch (Exception $e) { //we have an exception error, send http 500, log error error_log(sprintf("Error during saving nodes positions: %s", addslashes($e->getMessage())), 0); return $this->sendStateResponse(false, 500, "MongoDB save failure"); } //success return $this->sendStateResponse(true); }