/** * Checks for notifications for the current user * * Notifications are associative arrays with two keys: type and data. The type * describes what type the notification is. For example 'group_invitation. The * lay-out of the notification can then be determined client side. The data * differs per type and its format is dictated by the client side scripts * handling the type. This can for example be an array with the group name and * display name of the group you are invited for. * * @param BeeHub_Auth $auth An instance of the authentication class that can be used to determine the current user * @return array An array with notifications. */ public static function notifications(BeeHub_Auth $auth) { $notifications = array(); if ($auth->is_authenticated()) { $currentUser = $auth->current_user(); // Fetch all group invitations $groupsCollection = BeeHub::getNoSQL()->groups; $invitationsResultSet = $groupsCollection->find(array('admin_accepted_memberships' => $currentUser->name), array('name' => true, 'displayname' => true)); foreach ($invitationsResultSet as $row) { $notifications[] = array('type' => 'group_invitation', 'data' => array('group' => BeeHub::GROUPS_PATH . $row['name'], 'displayname' => $row['displayname'])); } // Fetch all group membership requests $groupRequestsResultSet = $groupsCollection->find(array('user_accepted_memberships' => array('$exists' => true), 'admins' => $currentUser->name), array('name' => true, 'displayname' => true, 'user_accepted_memberships' => true)); foreach ($groupRequestsResultSet as $group) { foreach ($group['user_accepted_memberships'] as $user_name) { $user = DAV::$REGISTRY->resource(BeeHub::USERS_PATH . $user_name); $notifications[] = array('type' => 'group_request', 'data' => array('group' => BeeHub::GROUPS_PATH . $group['name'], 'group_displayname' => $group['displayname'], 'user' => $user->path, 'user_displayname' => $user->user_prop_displayname(), 'user_email' => $user->user_prop(BeeHub::PROP_EMAIL))); } } // If the user doesn't have a sponsor, he can't do anything. if (count($currentUser->user_prop_sponsor_membership()) === 0) { $notifications[] = array('type' => 'no_sponsor', 'data' => array()); } else { // Fetch all sponsor membership requests $sponsorsCollection = BeeHub::getNoSQL()->sponsors; $sponsorRequestsResultSet = $sponsorsCollection->find(array('user_accepted_memberships' => array('$exists' => true), 'admins' => $currentUser->name), array('name' => true, 'displayname' => true, 'user_accepted_memberships' => true)); foreach ($sponsorRequestsResultSet as $sponsor) { foreach ($sponsor['user_accepted_memberships'] as $user_name) { $user = DAV::$REGISTRY->resource(BeeHub::USERS_PATH . $user_name); $notifications[] = array('type' => 'sponsor_request', 'data' => array('sponsor' => BeeHub::SPONSORS_PATH . $sponsor['name'], 'sponsor_displayname' => $sponsor['displayname'], 'user' => $user->path, 'user_displayname' => $user->user_prop_displayname(), 'user_email' => $user->user_prop(BeeHub::PROP_EMAIL))); } } } // end else for if ( count( $user->user_prop_sponsor_membership() ) === 0 ) } // end if ($auth->is_authenticated()) return $notifications; }
public function testHandle_authenticationSimpleSaml() { unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); // First test when not logged in yet, but when we want to login using SimpleSaml $simpleSamlLogin = $this->getMock('SimpleSAML_Auth_Simple', array('login', 'isAuthenticated'), array('BeeHub')); $simpleSamlLogin->expects($this->once())->method('login'); $simpleSamlLogin->expects($this->any())->method('isAuthenticated')->will($this->returnValue(false)); $_GET['login'] = '******'; $objLogin = new BeeHub_Auth($simpleSamlLogin); $objLogin->handle_authentication(); // And test once when simpleSaml is logged in $simpleSaml = $this->getMock('SimpleSAML_Auth_Simple', array('getAuthData', 'isAuthenticated'), array('BeeHub')); $simpleSaml->expects($this->once())->method('getAuthData')->with($this->equalTo('saml:sp:NameID'))->will($this->returnValue(array('Value' => 'qwertyuiop'))); $simpleSaml->expects($this->any())->method('isAuthenticated')->will($this->returnValue(true)); $_GET['login'] = '******'; $obj = new BeeHub_Auth($simpleSaml); $obj->handle_authentication(); $this->assertSame('/system/users/jane', $obj->current_user()->path, 'BeeHub_Auth::current_user() should now be set to the principal path of Jane Doe'); $this->assertTrue($obj->is_authenticated(), 'BeeHub_Auth::is_authenticated() should be true when Jane Doe is logged in'); $this->assertTrue($obj->surfconext(), 'BeeHub_Auth::surfconext() should return the true when Jane Doe logs in through SimpleSaml'); // And test once when simpleSaml is logged in $simpleSamlUnknown = $this->getMock('SimpleSAML_Auth_Simple', array('login', 'getAuthData', 'isAuthenticated'), array('BeeHub')); $simpleSamlUnknown->expects($this->once())->method('getAuthData')->with($this->equalTo('saml:sp:NameID'))->will($this->returnValue(array('Value' => 'unknown id'))); $simpleSamlUnknown->expects($this->any())->method('isAuthenticated')->will($this->returnValue(true)); $_GET['login'] = '******'; $objUnknown = new BeeHub_Auth($simpleSamlUnknown); $this->setExpectedException('DAV_Status', null, \DAV::HTTP_TEMPORARY_REDIRECT); $objUnknown->handle_authentication(); }