function volunteersLookupByUsername($req, $res)
 {
     $org = $this->getOrgForAdmin($req, $res);
     if (!is_object($org)) {
         return $org;
     }
     $username = $req->query('username');
     $user = User::findOne(['where' => ['username' => $username]]);
     if ($user && $org->getRoleOfUser($user) >= Volunteer::ROLE_AWAITING_APPROVAL) {
         return $res->redirect($org->manageUrl() . '/volunteers/' . $user->id());
     }
     $req->setParams(['usernameNotFound' => true]);
     return $this->volunteersBrowse($req, $res);
 }
 /**
  * Adds a volunteer to the organization. If the volunteer is not a
  * member yet, then a temporary account will be created. This
  * will send an e-mail to the user.
  *
  * @param string $emailOrUsername
  *
  * @return Volunteer|false invited volunteer
  */
 public function inviteVolunteer($emailOrUsername)
 {
     $user = false;
     $isEmail = true;
     if (Validate::is($emailOrUsername, 'email')) {
         $user = User::findOne(['where' => ['user_email' => $emailOrUsername]]);
         $isEmail = true;
     } else {
         $user = User::findOne(['where' => ['username' => $emailOrUsername]]);
     }
     // create temporary user
     if (!$user && $isEmail) {
         $user = User::createTemporary(['user_email' => $emailOrUsername, 'invited_by' => $this->id()]);
     }
     if (!$user) {
         return false;
     }
     $isTemporary = $user->isTemporary();
     $volunteer = new Volunteer([$user->id(), $this->id()]);
     if ($volunteer->exists()) {
         return $volunteer;
     }
     $volunteer = new Volunteer();
     $volunteer->grantAllPermissions();
     $volunteer->create(['uid' => $user->id(), 'organization' => $this->id(), 'application_shared' => true, 'active' => true, 'role' => Volunteer::ROLE_VOLUNTEER]);
     $base = $this->app['base_url'];
     $orgName = $this->name;
     $ctaUrl = $isTemporary ? $base . 'signup?user_email=' . $user->user_email : $base . 'profile';
     $user->sendEmail('volunteer-invite', ['subject' => "{$orgName} has invited you as a volunteer", 'orgname' => $orgName, 'cta_url' => $ctaUrl]);
     return $volunteer;
 }
 public function callback($req, $res)
 {
     if ($req->query('error_reason')) {
         return $res->redirect('/');
     }
     // generate forceLogin redirect_uri
     if ($req->query('forceLogin')) {
         $this->app['config']->set('instagram.redirect_uri', $this->app['config']->get('instagram.redirect_uri') . '?forceLogin=t');
     }
     $instagram = $this->app['instagram'];
     /* authenticate the user with the instagram API */
     $authenticatedUser = false;
     try {
         if ($instagram->Users->Authorize($req->query('code'))) {
             $authenticatedUser = $instagram->Users->getCurrentUser();
         }
     } catch (\Exception $e) {
         $this->app['logger']->error($e);
     }
     if (!$authenticatedUser) {
         $this->app['errors']->push(['context' => 'user.login', 'error' => 'invalid_token', 'message' => 'Instagram: Login error. Please try again.']);
         $usersController = new \app\users\Controller($this->app);
         return $usersController->loginForm($req, $res);
     }
     /* fetch the user's full profile */
     try {
         $user_profile = $instagram->Users->Info($authenticatedUser['id'])->data;
     } catch (\Exception $e) {
         $this->app['logger']->error($e);
         return $res->setCode(500);
     }
     /* log the user in or kick off signup */
     $currentUser = $this->app['user'];
     $iid = $user_profile['id'];
     // generate parameters to update profile
     $profileUpdateArray = ['id' => $iid, 'access_token' => $instagram->getAccessToken()];
     // instagram id matches existing user?
     $user = User::findOne(['where' => ['instagram_id' => $iid]]);
     if ($user) {
         // check if we are dealing with a temporary user
         if (!$user->isTemporary()) {
             if ($user->id() != $currentUser->id()) {
                 if ($req->query('forceLogin') || !$currentUser->isLoggedIn()) {
                     // log the user in
                     $this->app['auth']->signInUser($user->id(), 'instagram');
                 } else {
                     // inform the user that the instagram account they are trying to
                     // connect belongs to someone else
                     return new View('switchingAccounts/instagram.tpl', ['title' => 'Switch accounts?', 'otherUser' => $user, 'otherProfile' => $user->instagramProfile()]);
                 }
             }
             $profile = new InstagramProfile($iid);
             // create or update the profile
             if ($profile->exists()) {
                 $profile->set($profileUpdateArray);
             } else {
                 $profile = new InstagramProfile();
                 $profile->create($profileUpdateArray);
             }
             // refresh profile from API
             $profile->refreshProfile($user_profile);
             return $this->finalRedirect($req, $res);
         } else {
             // show finish signup screen
             $req->setSession('iid', $iid);
             return $res->redirect('/signup/finish');
         }
     }
     if ($currentUser->isLoggedIn()) {
         // add to current user's account
         $currentUser->set('instagram_id', $iid);
     } else {
         // save this for later
         $req->setSession('iid', $iid);
     }
     $profile = new InstagramProfile($iid);
     // create or update the profile
     if ($profile->exists()) {
         $profile->set($profileUpdateArray);
     } else {
         // create profile
         $profile = new InstagramProfile();
         $profile->create($profileUpdateArray);
     }
     // refresh profile from API
     $profile->refreshProfile($user_profile);
     // get outta here
     if ($currentUser->isLoggedIn()) {
         $this->finalRedirect($req, $res);
     } else {
         $res->redirect('/signup/finish');
     }
 }
 private function loginOrRegister($fbid, $user_profile, $req, $res)
 {
     $currentUser = $this->app['user'];
     $facebook = $this->app['facebook'];
     // get friend count
     $friendCount = 0;
     try {
         $friends = $facebook->api('me/friends');
         $friendCount = count((array) U::array_value($friends, 'data'));
     } catch (\FacebookApiException $e) {
         $this->app['logger']->error($e);
     }
     // generate parameters to update profile
     $profileUpdateArray = ['id' => $fbid, 'access_token' => $facebook->getAccessToken(), 'friends_count' => $friendCount];
     // fbid matches existing user?
     $user = User::findOne(['where' => ['facebook_id' => $fbid]]);
     if ($user) {
         // check if we are dealing with a temporary user
         if (!$user->isTemporary()) {
             if ($user->id() != $currentUser->id()) {
                 if ($req->query('forceLogin') || !$currentUser->isLoggedIn()) {
                     // log the user in
                     $this->app['auth']->signInUser($user->id(), 'facebook');
                 } else {
                     $logoutNextUrl = $this->app['base_url'] . 'facebook/connect?logout=t';
                     // inform the user that the facebook account they are trying to connect
                     // belongs to someone else
                     return new View('switchingAccounts/facebook', ['title' => 'Switch accounts?', 'otherUser' => $user, 'otherProfile' => $user->facebookProfile(), 'logoutUrl' => $facebook->getLogoutUrl(['next' => $logoutNextUrl])]);
                 }
             }
             $profile = new FacebookProfile($fbid);
             // create or update the profile
             if ($profile->exists()) {
                 $profile->set($profileUpdateArray);
             } else {
                 $profile = new FacebookProfile();
                 $profile->create($profileUpdateArray);
             }
             // refresh profile from API
             $profile->refreshProfile($user_profile);
             return $this->finalRedirect($req, $res);
         } else {
             // show finish signup screen
             $req->setSessoin('fbid', $fbid);
             return $res->redirect('/signup/finish');
         }
     }
     if ($currentUser->isLoggedIn()) {
         // add to current user's account
         $currentUser->set('facebook_id', $fbid);
     } else {
         // save this for later
         $req->setSession('fbid', $fbid);
     }
     $profile = new FacebookProfile($fbid);
     // create or update the profile
     if ($profile->exists()) {
         $profile->set($profileUpdateArray);
     } else {
         $profile = new FacebookProfile();
         $profile->create($profileUpdateArray);
     }
     // refresh profile from API
     $profile->refreshProfile($user_profile);
     // get outta here
     if ($currentUser->isLoggedIn()) {
         $this->finalRedirect($req, $res);
     } else {
         $res->redirect('/signup/finish');
     }
 }
 /**
  * @depends testCreate
  */
 public function testInviteVolunteer()
 {
     // invite an existing user by an email address
     $this->assertInstanceOf('app\\volunteers\\models\\Volunteer', self::$org->inviteVolunteer('*****@*****.**'));
     $this->assertEquals(Volunteer::ROLE_VOLUNTEER, self::$org->getRoleOfUser(self::$user));
     // invite an existing user by an email address again
     $this->assertInstanceOf('app\\volunteers\\models\\Volunteer', self::$org->inviteVolunteer('*****@*****.**'));
     $this->assertEquals(Volunteer::ROLE_VOLUNTEER, self::$org->getRoleOfUser(self::$user));
     // invite an existing user by username
     $this->assertInstanceOf('app\\volunteers\\models\\Volunteer', self::$org->inviteVolunteer('testvolunteer2'));
     $this->assertEquals(Volunteer::ROLE_VOLUNTEER, self::$org->getRoleOfUser(self::$user2));
     // invite a non-existent user by email address
     $this->assertInstanceOf('app\\volunteers\\models\\Volunteer', self::$org->inviteVolunteer('*****@*****.**'));
     // check that user exists and is temporary
     self::$tempUser = User::findOne(['where' => ['user_email' => '*****@*****.**']]);
     $this->assertTrue(self::$tempUser->exists());
     $this->assertTrue(self::$tempUser->isTemporary());
     $this->assertEquals(self::$org->id(), self::$tempUser->invited_by);
     $this->assertEquals(Volunteer::ROLE_VOLUNTEER, self::$org->getRoleOfUser(self::$tempUser));
     // invite a non-existent user by username
     $this->assertFalse(self::$org->inviteVolunteer('badusername'));
 }