/**
  * (non-PHPdoc)
  * @see app/modules/AppKit/lib/auth/AppKitIAuthProvider#isAvailable()
  */
 public function isAvailable($uid, $authid = null)
 {
     $res = AppKitDoctrineUtil::createQuery()->select('COUNT(u.user_id) as cnt')->from('NsmUser u')->where('u.user_name=? and user_disabled=?', array($uid, 0))->execute(null, Doctrine::HYDRATE_ARRAY);
     if (isset($res[0]['cnt']) && $res[0]['cnt'] != "0" && (int) $res[0]['cnt'] === 1) {
         return true;
     }
     return false;
 }
 /**
  * (non-PHPdoc)
  * @see app/modules/AppKit/lib/auth/AppKitIAuthProvider#isAvailable()
  */
 public function isAvailable($uid, $authid = null)
 {
     $res = AppKitDoctrineUtil::createQuery()->select('COUNT(u.user_id) as cnt')->from('NsmUser u')->where('lower(u.user_authkey)=? and user_disabled=? and user_authsrc = ?', array(strtolower($uid), 0, 'auth_key'));
     $res = $res->execute(null, Doctrine::HYDRATE_ARRAY);
     if (isset($res[0]['cnt']) && $res[0]['cnt'] == 1) {
         return true;
     }
     return false;
 }
 /**
  * Clear session for user
  */
 public function clearSession()
 {
     $result = AppKitDoctrineUtil::createQuery()->select('session_entry_id, session_data')->from('NsmSession')->execute();
     foreach ($result as $session) {
         $m = array();
         // :"user_id";s:1:"1";
         if (preg_match('/"user_id";s:\\d+:"([^"]+)"/', $session->session_data, $m)) {
             foreach ($this->userIds as $userId) {
                 if ($m[1] == $userId) {
                     AppKitDoctrineUtil::createQuery()->delete('NsmSession')->andWhere('session_entry_id=?', array($session->session_entry_id))->execute();
                 }
             }
         }
     }
 }
 /**
  * Reads data from doctrine tables and return its content
  * @param string $id
  * @throws AppKitDoctrineSessionStorageException
  */
 public function sessionRead($id)
 {
     $session_name = $this->getParameter('session_name');
     AppKitLogger::verbose("Reading session %s (id: %s)", $session_name, $id);
     $result = AppKitDoctrineUtil::createQuery()->select('*')->from('NsmSession n')->andWhere('session_id=? and session_name=?', array($id, $session_name))->execute();
     if ($result->count() == 0) {
         AppKitLogger::verbose("No session found, creating new ");
         $this->NsmSession = new NsmSession();
         $this->NsmSession->session_id = $id;
         $this->NsmSession->session_name = $session_name;
         // Immediately saving it empty
         $data = '';
         $this->sessionWrite($id, $data);
         return '';
     } else {
         AppKitLogger::verbose("Session found in database, reading data");
         $this->NsmSession = $result->getFirst();
         $data = $this->NsmSession->get('session_data');
         if (is_resource($data)) {
             AppKitLogger::verbose("Reading session from BLOB");
             $data = stream_get_contents($this->NsmSession->get('session_data'));
         }
         AppKitLogger::verbose("MD5 Check: %s == %s ", md5($data), $this->NsmSession->session_checksum);
         if (md5($data) == $this->NsmSession->session_checksum) {
             AppKitLogger::verbose("Using persisted session");
             return $data;
         }
         AppKitLogger::verbose("Session invalid, deleting it");
         $this->NsmSession->delete();
         throw new AppKitDoctrineSessionStorageException('Sessiondata integrity error, should be: ' . $this->NsmSession->session_checksum);
     }
 }
 public function executeRemove(AgaviRequestDataHolder $rd)
 {
     try {
         AppKitDoctrineUtil::getConnection()->beginTransaction();
         $roleadmin = $this->getContext()->getModel('RoleAdmin', 'AppKit');
         $padmin = $this->getContext()->getModel('PrincipalAdmin', 'AppKit');
         $ids = $rd->getParameter("ids", array());
         foreach ($ids as $id) {
             $role = $roleadmin->getRoleById($id);
             if (!$role) {
                 continue;
             }
             $roleadmin->removeRole($role);
         }
         AppKitDoctrineUtil::getConnection()->commit();
     } catch (Exception $e) {
         try {
             AppKitDoctrineUtil::getConnection()->rollback();
         } catch (Doctrine_Transaction_Exception $e) {
         }
         $this->setAttribute("error", $e->getMessage());
     }
     return 'Success';
 }
 /**
  * Drop existing permissions from category
  * @param CronkCategory $category
  */
 private function dropGroupPrincipals(CronkCategory $category)
 {
     $principals = AppKitDoctrineUtil::createQuery()->select('p.principal_id')->from('NsmPrincipal p')->innerJoin('p.categories c WITH c.cc_id = ?', array($category->cc_id))->andWhere('p.principal_type=?', array(NsmPrincipal::TYPE_ROLE))->execute();
     if ($principals->count()) {
         $in = array();
         foreach ($principals as $principal) {
             $in[] = $principal->principal_id;
         }
         $delete = AppKitDoctrineUtil::createQuery()->delete('CronkPrincipalCategory c')->andWhere('c.category_id=?', array($category->cc_id))->andWhereIn('c.principal_id', $in)->execute();
     }
     $category->refresh(true);
 }
 public function executeWrite(AgaviRequestDataHolder $rd)
 {
     // We need the execute method to work with parameter od the request!
     try {
         AppKitDoctrineUtil::getConnection()->beginTransaction();
         /** @var $useradmin AppKit_UserAdminModel **/
         $useradmin = $this->getContext()->getModel('UserAdmin', 'AppKit');
         /** @var $padmin AppKit_PrincipalAdminModel **/
         $padmin = $this->getContext()->getModel('PrincipalAdmin', 'AppKit');
         if ($rd->getParameter('id') == 'new') {
             $user = new NsmUser();
         } else {
             $user = $useradmin->getUserById($rd->getParameter('id'));
         }
         $useradmin->updateUserData($user, $rd);
         if ($rd->getParameter('password_validate', false) !== false) {
             $useradmin->updateUserPassword($user, $rd->getParameter('password_validate'));
         }
         // Updating the roles
         $useradmin->updateUserroles($user, $rd->getParameter('userroles', array()));
         $padmin->updatePrincipalValueData($user->principal, $rd->getParameter('principal_target', array()), $rd->getParameter('principal_value', array()));
         AppKitDoctrineUtil::getConnection()->commit();
     } catch (Exception $e) {
         try {
             AppKitDoctrineUtil::getConnection()->rollback();
         } catch (Doctrine_Transaction_Exception $e) {
         }
         print_r($e->getTraceAsString());
         $this->setAttribute("error", $e->getMessage());
     }
     return 'Success';
 }
 public function deleteCronkRecord($cronkid, $cronkname, $own = true)
 {
     if ($this->agaviUser->hasCredential('icinga.cronk.custom') === false && $this->agaviUser->hasCredential('icinga.cronk.admin') === false) {
         throw new AppKitModelException('No access to delete cronks!');
     }
     $q = AppKitDoctrineUtil::createQuery()->select('c.*')->from('Cronk c')->where('c.cronk_uid=?', array($cronkid));
     if ($own == true && $this->agaviUser->hasCredential('icinga.cronk.admin') === false) {
         $q->andWhere('c.cronk_user_id=?', array($this->user->user_id));
     }
     $cronk = $q->execute()->getFirst();
     if ($cronk instanceof Cronk && $cronk->cronk_id > 0) {
         AppKitDoctrineUtil::getConnection()->beginTransaction();
         $params = array($cronk->cronk_id);
         AppKitDoctrineUtil::createQuery()->delete('CronkCategoryCronk c')->andWhere('c.ccc_cronk_id=?')->execute($params);
         AppKitDoctrineUtil::createQuery()->delete('CronkPrincipalCronk c')->andWhere('c.cpc_cronk_id=?')->execute($params);
         AppKitDoctrineUtil::getConnection()->commit();
         $cronk->delete();
         return true;
     } else {
         throw new AppKitModelException('Could not delete cronk: ' . $cronkid);
     }
 }
예제 #9
0
 public function getTargetValuesArray()
 {
     /* removed caching for target values due to problems on deletion -mfrosch
        if (empty(self::$targetValuesCache)) {
            self::$targetValuesCache = $this->getStorage()->read("appkit.nsm_user.targetvalues");
        }
        */
     $userPrincipals = $this->getUserPrincipalsList(true);
     /*
     if (empty(self::$targetValuesCache)) {
     */
     $tc = AppKitDoctrineUtil::createQuery()->select('t.target_name, t.target_id')->from('NsmTarget t')->innerJoin('t.NsmPrincipalTarget pt')->andWhereIn('pt.pt_principal_id', $userPrincipals)->execute();
     $out = array();
     foreach ($tc as $t) {
         $out[$t->target_name] = array();
         $ptc = AppKitDoctrineUtil::createQuery()->from('NsmPrincipalTarget pt')->innerJoin('pt.NsmTargetValue tv')->andWhereIn('pt.pt_principal_id', $userPrincipals)->andWhere('pt.pt_target_id=?', array($t->target_id))->execute();
         foreach ($ptc as $pt) {
             $tmp = array();
             foreach ($pt->NsmTargetValue as $tv) {
                 $tmp[$tv->tv_key] = $tv->tv_val;
             }
             $out[$t->target_name][] = $tmp;
         }
     }
     /* removed caching for target values due to problems on deletion -mfrosch
            self::$targetValuesCache =& $out;
            $this->getStorage()->write("appkit.nsm_user.targetvalues",self::$targetValuesCache);
        }
        return self::$targetValuesCache;
        */
     return $out;
 }
 public function executeHtml(AgaviRequestDataHolder $rd)
 {
     $result = AppKitDoctrineUtil::createQuery()->from('NsmTarget')->where("target_type = ?")->execute(array('credential'), Doctrine_Core::HYDRATE_ARRAY);
     $this->setAttribute('principals', $result);
     $this->setupHtml($rd);
 }
 /**
  * Create of update a category
  * @param array $cat
  * @return Ambigous <NULL, CronkCategory>
  */
 public function createCategory(array $cat)
 {
     AppKitArrayUtil::swapKeys($cat, self::$cat_map, true);
     $category = null;
     if ($this->agaviUser->hasCredential('icinga.cronk.category.admin') && isset($cat['cc_uid'])) {
         $category = AppKitDoctrineUtil::createQuery()->from('CronkCategory cc')->andWhere('cc.cc_uid=?', $cat['cc_uid'])->execute()->getFirst();
     }
     if (!$category instanceof CronkCategory || !$category->cc_id > 0) {
         $category = new CronkCategory();
     }
     $category->fromArray($cat);
     $category->save();
     return $category;
 }
 /**
  * Remove all principals from a cronk
  * @param Cronk $cronk
  */
 private function dropGroupPrincipals(Cronk $cronk)
 {
     $principals = AppKitDoctrineUtil::createQuery()->select('p.principal_id')->from('NsmPrincipal p')->innerJoin('p.cronks c WITH c.cronk_id=?', $cronk->cronk_id)->andWhere('p.principal_type=?', array(NsmPrincipal::TYPE_ROLE))->execute();
     $pids = array();
     foreach ($principals as $principal) {
         $pids[] = $principal->principal_id;
     }
     if (count($pids) > 0) {
         AppKitDoctrineUtil::createQuery()->delete('CronkPrincipalCronk cpc')->andWhere('cpc.cpc_cronk_id=?', array($cronk->cronk_id))->andWhereIn('cpc.cpc_principal_id', $pids)->execute();
     }
     $cronk->refresh(true);
 }
 /**
  * Toggles the role status
  * @param NsmRole $role
  * @return boolean
  * @throws AppKitException
  * @author Marius Hein
  */
 public function toggleActivity(NsmRole &$role)
 {
     AppKitDoctrineUtil::toggleRecordValue($role);
     $role->save();
     return true;
 }
예제 #14
0
 public function getTargetValuesArray()
 {
     if (count(self::$targetValuesCache) == 0) {
         $tc = AppKitDoctrineUtil::createQuery()->select('t.target_name, t.target_id')->from('NsmTarget t')->innerJoin('t.NsmPrincipalTarget pt')->andWhereIn('pt.pt_principal_id', $this->getPrincipalsList())->execute();
         $out = array();
         foreach ($tc as $t) {
             $out[$t->target_name] = array();
             $ptc = AppKitDoctrineUtil::createQuery()->from('NsmPrincipalTarget pt')->innerJoin('pt.NsmTargetValue tv')->andWhereIn('pt.pt_principal_id', $this->getPrincipalsList())->andWhere('pt.pt_target_id=?', array($t->target_id))->execute();
             foreach ($ptc as $pt) {
                 $tmp = array();
                 foreach ($pt->NsmTargetValue as $tv) {
                     $tmp[$tv->tv_key] = $tv->tv_val;
                 }
                 $out[$t->target_name][] = $tmp;
             }
         }
         self::$targetValuesCache =& $out;
     }
     return self::$targetValuesCache;
 }
 public function removeUser(NsmUser &$user)
 {
     try {
         /*
          * These are our connections to any cronks
          */
         foreach ($user->cronkPrincipals as $cp) {
             $re = AppKitDoctrineUtil::createQuery()->delete('CronkPrincipalCronk cpc')->andWhere('cpc.cpc_cronk_id=? and cpc.cpc_principal_id=?', array($cp->cpc_cronk_id, $cp->cpc_principal_id))->execute();
         }
         /*
          * Our cronks
          */
         foreach ($user->cronks as $cronk) {
             /*
              * All connections to our cronks
              */
             AppKitDoctrineUtil::createQuery()->delete('CronkPrincipalCronk cpc')->andWhere('cpc.cpc_cronk_id=?', array($cronk->cronk_id))->execute();
             $cronk->delete();
         }
         AppKitDoctrineUtil::getConnection()->beginTransaction();
         $this->updateUserroles($user, array());
         $targets = $user->getTargets();
         foreach ($targets as $target) {
             $vals = $user->getTargetValues($target->get("target_name"));
             foreach ($vals as $value) {
                 $value->delete();
             }
         }
         $principals = $user->getPrincipals();
         if (!$principals instanceof NsmPrincipal) {
             foreach ($principals as $pr) {
                 if ($pr->NsmPrincipalTarget) {
                     foreach ($pr->NsmPrincipalTarget as $pr_t) {
                         $pr_t->delete();
                     }
                 }
                 $pr->delete();
             }
         } else {
             if ($principals->NsmPrincipalTarget) {
                 foreach ($principals->NsmPrincipalTarget as $pr_t) {
                     $pr_t->delete();
                 }
             }
             $principals->delete();
         }
         $user->delete();
         AppKitDoctrineUtil::getConnection()->commit();
         return true;
     } catch (Exception $e) {
         AppKitDoctrineUtil::getConnection()->rollback();
         $this->getContext()->getLoggerManager()->log($e->getMessage());
         throw $e;
     }
 }
 private function deleteAllPrincipalTargetEntries(NsmPrincipal &$p)
 {
     AppKitDoctrineUtil::getConnection()->beginTransaction();
     foreach ($p->NsmPrincipalTarget as $pt) {
         $pt->NsmTargetValue->delete();
         $pt->delete();
     }
     AppKitDoctrineUtil::getConnection()->commit();
     return true;
 }
 /**
  * Returns a safe log collection
  * @param integer $limit
  * @return Doctrine_Query
  * @author Marius Hein
  */
 public function getLogQuery($limit = 1000)
 {
     return AppKitDoctrineUtil::createQuery()->from('NsmLog')->limit('1000')->orderBy('log_created DESC');
 }