function users_register_post() { $u = new Users_User(); $u->email_address = $_REQUEST['email_address']; if ($u->retrieve()) { $key = 'this email'; throw new Users_Exception_AlreadyVerified(compact('key')); } // Insert a new user into the database $user = new Users_User(); $user->username = $_REQUEST['username']; if ($user->retrieve()) { throw new Users_Exception_UsernameExists(array(), array('username')); } $user->icon = 'default'; $user->password_hash = ''; $user->save(); // sets the user's id // Import the user's icon if (isset($_REQUEST['icon'])) { $folder = 'user_id_' . $user->id; users_register_post_download($_REQUEST['icon'], $folder, 80); users_register_post_download($_REQUEST['icon'], $folder, 40); $user->icon = $folder; $user->save(); } // Add an email to the user, that they'll have to verify $user->addEmail($_REQUEST['email_address']); Users::setLoggedInUser($user); Users::$cache['user'] = $user; }
function Users_0_8_3_Users_mysql() { $app = Q_Config::expect('Q', 'app'); $appRootUrl = Q_Config::expect('Q', 'web', 'appRootUrl'); $user = new Users_User(); $user->id = $app; $user->username = $app; $user->url = $appRootUrl; $user->icon = "{$appRootUrl}/img/icon"; $user->signedUpWith = 'none'; $user->save(); }
function Users_0_8_3_Users_mysql() { $app = Q_Config::expect('Q', 'app'); $communityId = Users::communityId(); $communityName = Q_Config::get('Users', 'community', 'name', $app); $appRootUrl = Q_Config::expect('Q', 'web', 'appRootUrl'); $user = new Users_User(); $user->id = $communityId; $user->username = $communityName; $user->url = $appRootUrl; $user->icon = "{$appRootUrl}/img/icon"; $user->signedUpWith = 'none'; $user->save(); }
/** * Returns a user in the database that will correspond to a new user in the future * once they authenticate or follow an invite. * Inserts a new user if one doesn't already exist. * * @method futureUser * @param {string} $type Could be one of "email", "mobile", "email_hashed", "mobile_hashed", "facebook", "twitter" or "none". * @param {string} $value The value corresponding to the type. If $type is: * * * "email" - this is one of the user's email addresses * * "mobile" - this is one of the user's mobile numbers * * "email_hashed" - this is the email, already hashed with Q_Utils::hash() * * "mobile_hashed" - this is the email, already hashed with Q_Utils::hash() * * "facebook" - this is the user's id on facebook * * "twitter" - this is the user's id on twitter * * "none" - the type is ignored, no "identify" rows are inserted into the db, etc. * * With every type except "none", the user will be * * NOTE: If the person we are representing here comes and registers the regular way, * and then later adds an email, mobile, or authenticates with a provider, * which happens to match the "future" mapping we inserted in users_identify table, * then this futureUser will not be converted, since they already registered * a different user. Later on, we may have some sort function to merge users together. * * @param {&string} [$status=null] The status of the user - 'verified' or 'future' * @return {Users_User} * @throws {Q_Exception_WrongType} If $type is not supported * @throws {Q_Exception_MissingRow} If identity for user exists but user does not exists */ static function futureUser($type, $value, &$status = null) { if (!array_key_exists($type, self::$types)) { throw new Q_Exception_WrongType(array('field' => 'type', 'type' => 'one of the supported types')); } if ($type !== 'none') { $ui = Users::identify($type, $value, null); if ($ui && !empty($ui->userId)) { $user = new Users_User(); $user->id = $ui->userId; if ($user->retrieve()) { $status = $ui->state; return $user; } else { $userId = $ui->userId; throw new Q_Exception_MissingRow(array('table' => 'user', 'criteria' => 'that id'), 'userId'); } } } // Make a user row to represent a "future" user and give them an empty username $user = new Users_User(); if ($field = self::$types[$type]) { $user->{$field} = $value; } $user->signedUpWith = 'none'; // this marks it as a future user for now $user->username = ""; $user->icon = 'future'; $during = 'future'; /** * @event Users/insertUser {before} * @param {string} during * @param {Users_User} 'user' */ Q::event('Users/insertUser', compact('user', 'during'), 'before'); $user->save(); // sets the user's id /** * @event Users/insertUser {after} * @param {string} during * @param {Users_User} user */ Q::event('Users/insertUser', compact('user', 'during'), 'after'); if ($type != 'email' and $type != 'mobile') { if ($type !== 'none') { // Save an identifier => user pair for this future user $ui = new Users_Identify(); $ui->identifier = "{$type}:{$value}"; $ui->state = 'future'; if (!$ui->retrieve()) { $ui->userId = $user->id; $ui->save(); } $status = $ui->state; } else { $status = 'future'; } } else { // Save hashed version $ui = new Users_Identify(); $hashed = Q_Utils::hash($value); $ui->identifier = $type . "_hashed:{$hashed}"; $ui->state = 'future'; if (!$ui->retrieve()) { $ui->userId = $user->id; $ui->save(); } $status = $ui->state; } return $user; }
/** * Logs a user in using a login identifier and a pasword * @param string $identifier * Could be an email address, a mobile number, or a user id. * @param string $password * The password to hash, etc. */ static function login($identifier, $password) { $return = null; $return = Pie::event('users/login', compact('identifier', 'password'), 'before'); if (isset($return)) { return $return; } Pie_Session::start(); $session_id = Pie_Session::id(); // First, see if we've already logged in somehow if ($user = self::loggedInUser()) { // Get logged in user from session return $user; } $user = new Users_User(); $user->identifier = $identifier; if ($user->retrieve()) { // User exists in database. Now check the password. $password_hash = self::hashPassword($password, $user->password_hash); if ($password_hash != $user->password_hash) { // Passwords don't match! throw new Users_Exception_WrongPassword(compact('identifier')); } // Do we need to update it? if (!isset($user->session_key) or $user->session_key != $session_id) { Pie::event('users/loginUpdateUser', compact('user'), 'before'); $user->session_key = $session_id; $user->save(); // update session_key in user Pie::event('users/loginUpdateUser', compact('user'), 'after'); } } else { // No user in database. Will insert a new one! // These users might be shared across apps. $user->password_hash = self::hashPassword($password); $user->session_key = $session_id; Pie::event('users/loginInsertUser', compact('user'), 'before'); $user->save(); Pie::event('users/loginInsertUser', compact('user'), 'after'); $inserted_new_user = true; } // Now save this user in the session as the logged-in user self::setLoggedInUser($user); Pie::event('users/login', compact('identifier', 'password', 'inserted_new_user', 'user'), 'after'); }