/**
  * Create and return a data source to fetch all groups from all backends where the user is not already a member of
  *
  * @return  ArrayDatasource
  */
 protected function createDataSource()
 {
     $groups = $failures = array();
     foreach ($this->backends as $backend) {
         try {
             $memberships = $backend->select()->from('group_membership', array('group_name'))->where('user_name', $this->userName)->fetchColumn();
             foreach ($backend->select(array('group_name')) as $row) {
                 if (!in_array($row->group_name, $memberships)) {
                     // TODO(jom): Apply this as native query filter
                     $row->backend_name = $backend->getName();
                     $groups[] = $row;
                 }
             }
         } catch (Exception $e) {
             $failures[] = array($backend->getName(), $e);
         }
     }
     if (empty($groups) && !empty($failures)) {
         // In case there are only failures, throw the very first exception again
         throw $failures[0][1];
     } elseif (!empty($failures)) {
         foreach ($failures as $failure) {
             Logger::error($failure[1]);
             Notification::warning(sprintf($this->translate('Failed to fetch any groups from backend %s. Please check your log'), $failure[0]));
         }
     }
     return new ArrayDatasource($groups);
 }
 /**
  * Fetch and return all users from all user backends
  *
  * @return  ArrayDatasource
  */
 protected function fetchUsers()
 {
     $users = array();
     foreach ($this->loadUserBackends('Icinga\\Data\\Selectable') as $backend) {
         try {
             foreach ($backend->select(array('user_name')) as $row) {
                 $users[] = $row;
             }
         } catch (Exception $e) {
             Logger::error($e);
             Notification::warning(sprintf($this->translate('Failed to fetch any users from backend %s. Please check your log'), $backend->getName()));
         }
     }
     return new ArrayDatasource($users);
 }
 /**
  * Fetch and return the given user's groups from all user group backends
  *
  * @param   User    $user
  *
  * @return  ArrayDatasource
  */
 protected function loadMemberships(User $user)
 {
     $groups = $alreadySeen = array();
     foreach ($this->loadUserGroupBackends() as $backend) {
         try {
             foreach ($backend->getMemberships($user) as $groupName) {
                 if (array_key_exists($groupName, $alreadySeen)) {
                     continue;
                     // Ignore duplicate memberships
                 }
                 $alreadySeen[$groupName] = null;
                 $groups[] = (object) array('group_name' => $groupName, 'backend' => $backend);
             }
         } catch (Exception $e) {
             Logger::error($e);
             Notification::warning(sprintf($this->translate('Failed to fetch memberships from backend %s. Please check your log'), $backend->getName()));
         }
     }
     return new ArrayDatasource($groups);
 }