Skip to content
Browse files

Fixed SQL query to retrieve session user

  • Loading branch information...
1 parent 3439d13 commit 8e4dd6fdcf50f5afe6586eb8a0fc036a063f8887 @chanmix51 committed Mar 19, 2012
View
14 README.rst
@@ -213,4 +213,16 @@ When generating the model files, you must specifically rebuild the base file for
'parent_namespace' => '\GHub\Silex\PommGuard\Model'
));
-By default, entity classes extend ``Pomm\Object\BaseObject``, change ``MyUser`` class to extend ``\GHub\Silex\PommGuard\Model\PommUser`` and you're done.
+By default, entity classes extend ``Pomm\Object\BaseObject``, change ``MyUser`` class to extend ``\GHub\Silex\PommGuard\Model\PommUser`` and you're done.
+
+If you also overload the ``PommGroup`` class, you have to tell ``PommUser`` of it in the ``initialize`` method of your ``PommUserMap`` class::
+
+ public function initialize()
+ {
+ parent::initialize();
+
+ $this->group_map = $this->connection
+ ->getMapForm('\Your\Schema\GroupEntity');
+ }
+
+Of course, this group map class has to extend the base class provided by PommGuard as it expects to have at least the given structure.
View
24 src/GHub/Silex/PommGuard/Model/PommUserMap.php
@@ -57,7 +57,7 @@ public function findByPkWithAcls(Array $pk)
$sql = <<<_
WITH
- acls (name) AS (
+ raw_acls (name) AS (
SELECT DISTINCT
unnest(credentials)
FROM
@@ -67,30 +67,36 @@ public function findByPkWithAcls(Array $pk)
g.name = ANY(v.groups)
AND
%s
+ ),
+ agg_acls (acls) AS (
+ SELECT
+ CASE
+ WHEN count(name) = 0 THEN ARRAY[]::varchar[]
+ ELSE array_agg(name)
+ END AS acls
+ FROM
+ raw_acls
)
SELECT
%s,
- array_agg(a.name) AS credentials
+ a.acls AS credentials
FROM
%s v,
- acls a
+ agg_acls a
WHERE
%s
-GROUP BY
- %s
_;
$sql = sprintf($sql,
$this->getTableName(),
$this->group_map->getTableName(),
(string) $where,
- $this->getSelectFields('v'),
+ join(', ', $this->getSelectFields('v')),
$this->getTableName(),
- (string) $where,
- $this->getGroupByFields('v')
+ (string) $where
);
- $this->addVirtualField('credentials', 'String');
+ $this->addVirtualField('credentials', 'varchar[]');
$pomm_users = $this->query($sql, array_merge($where->getValues(), $where->getValues()));
return (!$pomm_users->isEmpty()) ? $pomm_users->current() : null;
View
4 src/GHub/Silex/PommGuard/PommGuardServiceProvider.php
@@ -47,9 +47,7 @@ public function register(Application $app)
return new Session($app['session.storage']);
});
- $app['session']->setUserMap($app['pomm']
- ->getDatabase($app['pomm_guard.config.database'])
- ->createConnection()
+ $app['session']->setUserMap($app['pomm_guard.config.connection']
->getMapFor($app['pomm_guard.config.user']));
}
}
View
18 src/GHub/Silex/PommGuard/Session.php
@@ -20,31 +20,19 @@ public function setPommUser(Model\PommUser $user)
{
$this->pomm_guard_user = $user;
- foreach ($this->user_map->getPrimaryKey() as $key)
- {
- $this->set(sprintf('_pg_%s', $key), $user[$key]);
- }
+ $this->set('_pg_guard_user', $user->get($this->user_map->getPrimaryKey()));
}
public function removePommUser()
{
- foreach ($this->user_map->getPrimaryKey() as $key)
- {
- $this->remove(sprintf('_pg_%s', $key));
- }
+ $this->remove('_pg_guard_user');
}
public function getPommUser()
{
if (is_null($this->pomm_guard_user))
{
- $pk = array();
- foreach ($this->user_map->getPrimaryKey() as $key)
- {
- $pk[$key] = $this->get(sprintf('_pg_%s', $key));
- }
-
- $this->pomm_guard_user = $this->user_map->findByPkWithAcls($pk);
+ $this->pomm_guard_user = $this->user_map->findByPkWithAcls($this->get('_pg_guard_user'));
}
return $this->pomm_guard_user;

0 comments on commit 8e4dd6f

Please sign in to comment.
Something went wrong with that request. Please try again.