public function action_index() { $count = ORM::factory('User')->count_all(); if ($count === 0) { $this->template->content = View::factory('install/index'); if ($this->request->method() === Request::POST) { if (!Security::check($this->request->param('id'))) { throw new Exception("Bad token!"); } $post = Validation::factory($_POST)->rule('username', 'not_empty')->rule('email', 'not_empty')->rule('email', 'email')->rule('password', 'not_empty')->rule('password', 'min_length', array(':value', '8'))->rule('password2x', 'not_empty')->rule('password', 'matches', array(':validation', 'password', 'password2x')); if ($post->check()) { $user = new Model_User(); $post = $this->request->post(); $user->values($post)->save(); $adminRole = ORM::factory('Role')->where('name', '=', 'admin')->find(); $loginRole = ORM::factory('Role')->where('name', '=', 'login')->find(); $user->add('roles', $loginRole); $user->add('roles', $adminRole); $this->redirect('install/successful'); } else { $this->redirect('install/oops'); } } } else { $this->redirect(''); } }
public function action_index() { $view = View::factory('register'); if ($this->request->method() === Request::POST) { if (!Security::check($this->request->param('id'))) { throw new Exception("Bad token!"); } $post = Validation::factory($_POST)->rule('username', 'not_empty')->rule('username', 'Model_User::is_username_taked')->rule('email', 'not_empty')->rule('email', 'Model_User::unique_email')->rule('email', 'email')->rule('password', 'not_empty')->rule('password', 'min_length', array(':value', '8'))->rule('password_again', 'not_empty')->rule('password', 'matches', array(':validation', 'password', 'password_again')); if ($post->check()) { $user = new Model_User(); $post = $this->request->post(); $user->values($post)->save(); // atrod role 'login' $loginRole = ORM::factory('Role')->where('name', '=', 'login')->find(); // pieliek klāt 'login' role litotājam $user->add('roles', $loginRole); $this->template->content = $view->bind('successful', $this->true); } else { $this->template->content = $view->bind('errors', $this->true); } } $this->template->content = $view->render(); }
public function action_adduser() { if (!empty($_SESSION['kids_id'])) { if (empty($_SESSION['kidsData'])) { $_SESSION['kidData'] = Helper_Mmdb::getUserData($_SESSION['kids_id']); } $user = new Model_User(); $userData = array(); $userData['username'] = Model_User::create_unique_display_name(); $userData['password'] = !empty($_SESSION['loginInfo']['password']) ? $_SESSION['loginInfo']['password'] : Model_User::create_unique_display_name(); $userData['password_confirm'] = $userData['password']; $userData['birthdate'] = $_POST['birthdate']; $userData['gender'] = $_POST['gender']; $userData['display_name'] = $_POST['display_name']; $userData['email'] = $_POST['email']; if (empty($_POST['vanity_url'])) { $userData['vanity_url'] = strtolower(preg_replace("/[^A-Za-z0-9 ]/", "-", $userData['display_name'])); } else { $userData['vanity_url'] = $_POST['vanity_url']; } //check vanity for uniqueness $checkVanity = ORM::factory("user")->where("vanity_url", "=", $userData['vanity_url'])->find(); $vanId = 1; while ($checkVanity->loaded()) { $checkVanity = ORM::factory("user")->where("vanity_url", "=", $userData['vanity_url'] . "-{$vanId}")->find(); if (!$checkVanity->loaded()) { $userData['vanity_url'] .= "-{$vanId}"; break; } $vanId++; } /* Load the validation rules, filters etc.*/ $post = $user->validate_create($userData); /* If the post data validates using the rules setup in the user model. */ if ($post->check()) { /* Affects the sanitized vars to the user object. */ $user->values($post); /* Create the account. */ $user->kids_id = $_SESSION['kids_id']; $user->save(); /* Add the login role to the user. */ $login_role = new Model_Role(array('name' => 'login')); $user->add('roles', $login_role); /* Add approval. */ $this->setup_approval($user); /* Sign the user in. */ Auth::instance()->login($post['username'], $post['password']); /* Redirect to the user account. */ Request::instance()->redirect('profile'); } else { /* Get errors for display in view. */ Message::set(Message::ERROR, $post->errors('user')); $this->action_merge(); } } else { Request::instance()->redirect('account/signin'); } }
protected function add_edit(Model_User &$user) { $errors = array(); $roles = ORM::factory('Role')->where('id', '!=', Model_User::LOGIN_ROLE_ID)->order_by('id')->find_all()->as_array('id'); if ($this->request->method() == Request::POST) { $data = $this->request->post(); $_data = $data; // operate on copy: $_data $email = $user->email; // keep email in the case od validation exception to restore this value in $user $external_validation = Validation::factory($_data)->labels(array('repeat_email' => 'Repeat e-mail'))->rules('roles', array(array('each_in_array', array(':value', array_keys($roles)))))->rules('repeat_email', array(array('matches', array(':validation', ':field', 'email')))); if ($user->loaded()) { if (empty($_data['email'])) { // no email while editing means: no changing but ORM model need email value to be not empty $_data['email'] = $user->email; } } else { $_data['password'] = Text::random('alnum', 14); // set random password for new user } try { $user->values($_data)->save($external_validation); } catch (ORM_Validation_Exception $vex) { $errors = $vex->errors('orm'); $user->email = $email; // restore original email value } // Manage roles for user: if (empty($errors)) { $user_roles = (array) Arr::get($data, 'roles', array()); foreach ($roles as $role) { // Adding: if (in_array($role->id, $user_roles) and !$user->has_role($role->id)) { $user->add('roles', $role); } // Removing: if (!in_array($role->id, $user_roles) and $user->has_role($role->id)) { $user->remove('roles', $role); } } if (!empty($data['send_hashlink'])) { $this->send_activation($user); } // finish saving return TRUE; } } else { if ($user->loaded()) { $data = $user->as_array(); $data['email'] = ''; $data['roles'] = array(); foreach ($roles as $role) { if ($user->has_role($role->id)) { $data['roles'][] = $role->id; } } } else { $data = array('send_hashlink' => '1'); } } $this->content = View::factory("users/edit")->bind("user", $user)->bind("roles", $roles)->bind("data", $data)->bind("errors", $errors); }