Esempio n. 1
0
 private static function _createUser($profile, $tokens, $setSession = false)
 {
     // Add username
     $username = UsersModel::create();
     $username->google_id = $profile['id'];
     $username->name = $profile['displayName'];
     $username->email = $profile['email'];
     $username->last_login = null;
     $username->status = UsersModel::STATUS_ACTIVE;
     $username->group = static::_isLoggedIn() ? $_SESSION['current']['username']['group'] : md5($profile['id']);
     $username->save();
     // Credentials
     $credentials = UsersCredentialsModel::create();
     $credentials->user_id = $username->id;
     $credentials->access_token = $tokens['access_token'];
     $credentials->refresh_token = isset($tokens['refresh_token']) ? $tokens['refresh_token'] : null;
     $credentials->token_type = $tokens['token_type'];
     $credentials->expires_at = date(DATE_TIME, time() + $tokens['expires_in']);
     $credentials->raw = base64_encode(json_encode($tokens));
     $credentials->save();
     // Profile
     $userProfile = UsersProfilesModel::create();
     $userProfile->user_id = $username->id;
     $userProfile->type = $profile['kind'];
     $userProfile->gender = isset($profile['gender']) ? $profile['gender'] : '';
     $userProfile->url = isset($profile['link']) ? $profile['link'] : '';
     $userProfile->avatar = isset($profile['picture']) ? $profile['picture'] : '';
     $userProfile->birthday = isset($profile['birthday']) ? $profile['birthday'] : '';
     $userProfile->save();
     // Add mandatory services
     $services = ServicesModel::all(array('mandatory' => true, 'status' => ServicesModel::STATUS_ACTIVE))->column('id');
     if ($services) {
         foreach ($services as $service) {
             $userService = UsersServicesModel::create();
             $userService->user_id = $username->id;
             $userService->service_id = $service;
             $userService->save();
         }
     }
     $username = $username->toArray();
     $userProfile = $userProfile->toArray();
     $credentials = $credentials->toArray();
     $services = ServicesModel::forUser(array('id' => $username['id']));
     // Session thingie
     static::setUsername(compact('username', 'userProfile', 'credentials', 'services', 'setSession'));
 }