/** * Adds a device to the current user id and session. * See Users_Device::add method for more details. * @param {string} $deviceId * @return {void} */ function Users_device_post() { Q_Request::requireFields(array('deviceId')); $deviceId = $_REQUEST['deviceId']; $user = Users::loggedInUser(true); $device = Users_Device::add(array_merge($_REQUEST, array('userId' => $user->id))); Q_Response::setSlot('data', $device); }
function Users_oAuth_post() { // Validate the inputs $fields = array('response_type', 'token_type', 'access_token', 'expires_in', 'scope', 'state', 'Q_Users_oAuth'); Q_Request::requireFields($fields, true); $params = Q::take($_REQUEST, $fields); $params['Q.Users.oAuth'] = $params['Q_Users_oAuth']; unset($params['Q_Users_oAuth']); Q_Valid::signature(true, $params, array('Q.Users.oAuth')); // Set the session id to the access_token Q_Session::id($params['access_token']); // Add a device, if any if ($deviceId = Q::ifset($_REQUEST, 'deviceId', null)) { $fields2 = array('deviceId', 'platform', 'version', 'formFactor'); Q_Request::requireFields($fields2); $device = Q::take($_REQUEST, $fields2); $device['userId'] = Users::loggedInUser(true)->id; Users_Device::add($device); } }
/** * Registers a user in the system. * @method register * @static * @param {string} $username The name of the user * @param {string|array} $identifier Can be an email address or mobile number. Or it could be an array of $type => $info * @param {string} [$identifier.identifier] an email address or phone number * @param {array} [$identifier.device] an array with keys "deviceId", "platform", "version" * to store in the Users_Device table for sending notifications * @param {array|string} [$icon=array()] Array of filename => url pairs * @param {string} [$provider=null] Provider such as "facebook" * @param {array} [$options=array()] An array of options that could include: * @param {string} [$options.activation] The key under "Users"/"transactional" config to use for sending an activation message. Set to false to skip sending the activation message for some reason. * @return {Users_User} * @throws {Q_Exception_WrongType} If identifier is not e-mail or modile * @throws {Q_Exception} If user was already verified for someone else * @throws {Users_Exception_AlreadyVerified} If user was already verified * @throws {Users_Exception_UsernameExists} If username exists */ static function register($username, $identifier, $icon = array(), $provider = null, $options = array()) { if (is_array($provider)) { $options = $provider; $provider = null; } /** * @event Users/register {before} * @param {string} username * @param {string|array} identifier * @param {string} icon * @param {string} provider * @return {Users_User} */ $return = Q::event('Users/register', compact('username', 'identifier', 'icon', 'provider', 'options'), 'before'); if (isset($return)) { return $return; } $during = 'register'; if (is_array($identifier)) { reset($identifier); switch (key($identifier)) { case 'device': $fields = array('deviceId', 'platform', 'version'); Q_Valid::requireFields($fields, $identifier, true); $device = $identifier; if (isset($identifier['identifier'])) { $identifier = $identifier['identifier']; } break; default: throw new Q_Exception_WrongType(array('field' => 'identifier', 'type' => 'an array with entry named "device"')); } } if (Q_Valid::email($identifier, $emailAddress)) { $ui_identifier = $emailAddress; $key = 'email address'; $type = 'email'; } else { if (Q_Valid::phone($identifier, $mobileNumber)) { $key = 'mobile number'; $ui_identifier = $mobileNumber; $type = 'mobile'; } else { throw new Q_Exception_WrongType(array('field' => 'identifier', 'type' => 'email address or mobile number'), array('emailAddress', 'mobileNumber')); } } $user = false; if ($provider) { if ($provider != 'facebook') { throw new Q_Exception_WrongType(array('field' => 'provider', 'type' => '"facebook"')); } $facebook = Users::facebook(); if ($facebook) { $uid = $facebook->getUser(); try { // authenticate (and possibly adopt) an existing provider user // or insert a new user during this authentication $user = Users::authenticate($provider, null, $authenticated, true); } catch (Exception $e) { } if ($user) { // the user is also logged in $adopted = true; // Adopt this provider user /** * @event Users/adoptFutureUser {before} * @param {Users_User} user * @param {string} during * @return {Users_User} */ $ret = Q::event('Users/adoptFutureUser', compact('user', 'during'), 'before'); if ($ret) { $user = $ret; } } } } if (!$user) { $user = new Users_User(); // the user we will save in the database } if (empty($adopted)) { // We will be inserting a new user into the database, so check if // this identifier was already verified for someone else. $ui = Users::identify($type, $ui_identifier); if ($ui) { throw new Users_Exception_AlreadyVerified(compact('key'), array('emailAddress', 'mobileNumber', 'identifier')); } } // Insert a new user into the database, or simply modify an existing (adopted) user $user->username = $username; if (!isset($user->signedUpWith) or $user->signedUpWith == 'none') { $user->signedUpWith = $type; } $user->icon = 'default'; $user->passphraseHash = ''; $url_parts = parse_url(Q_Request::baseUrl()); if (isset($url_parts['host'])) { // By default, the user's url would be this: $user->url = $username ? "http://{$username}." . $url_parts['host'] : ""; } /** * @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'); $sizes = Q_Config::expect('Users', 'icon', 'sizes'); sort($sizes); if (empty($icon)) { switch ($provider) { case 'facebook': // let's get this user's icon on facebook if (empty($uid)) { break; } $icon = array(); foreach ($sizes as $size) { $icon["{$size}.png"] = "https://graph.facebook.com/{$uid}/picture?width={$size}&height={$size}"; } break; } } else { // Import the user's icon and save it if (is_string($icon)) { // assume it's from gravatar $iconString = $icon; $icon = array(); foreach ($sizes as $size) { $icon["{$size}.png"] = "{$iconString}&s={$size}"; } } else { // locally generated icons $hash = md5(strtolower(trim($identifier))); $icon = array(); foreach ($sizes as $size) { $icon["{$size}.png"] = array('hash' => $hash, 'size' => $size); } } } if (!Q_Config::get('Users', 'register', 'icon', 'leaveDefault', false)) { self::importIcon($user, $icon); $user->save(); } if (empty($user->emailAddress) and empty($user->mobileNumber)) { // Add an email address or mobile number to the user, that they'll have to verify try { $activation = Q::ifset($options, 'activation', 'activation'); if ($activation) { $subject = Q_Config::get('Users', 'transactional', $activation, "subject", null); $body = Q_Config::get('Users', 'transactional', $activation, "body", null); } else { $subject = $body = null; } if ($type === 'email') { $user->addEmail($identifier, $subject, $body, array(), $options); } else { if ($type === 'mobile') { $p = $options; if ($delay = Q_Config::get('Users', 'register', 'delaySms', 0)) { $p['delay'] = $delay; } $sms = Q_Config::get('Users', 'transactional', $activation, "sms", null); $user->addMobile($mobileNumber, $sms, array(), $p); } } } catch (Exception $e) { // The activation message could not be sent, so remove this user // from the database. This way, this username will be // back on the market. $user->remove(); throw $e; } } if (!empty($device)) { $device['userId'] = $user->id; Users_Device::add($device); } /** * @event Users/register {after} * @param {string} username * @param {string|array} identifier * @param {string} icon * @param {Users_User} user * @param {string} provider * @return {Users_User} */ $return = Q::event('Users/register', compact('username', 'identifier', 'icon', 'user', 'provider', 'options', 'device'), 'after'); return $user; }