/** * Save staff * * @param Newscoop\Entity\User\Staff $staff * @param array $values * @return void */ public function save(Staff $staff, array $values) { $em = $this->getEntityManager(); // set groups $groups = $staff->getGroups(); $groups->clear(); if (!empty($values['groups'])) { foreach ($values['groups'] as $groupId) { $group = $em->getReference('Newscoop\\Entity\\User\\Group', (int) $groupId); $groups->add($group); } } // try if will be able to manage users $acl = \Zend_Registry::get('acl')->getAcl($staff); $auth = \Zend_Auth::getInstance(); if ($auth->getIdentity() == $staff->getId() && !$acl->isAllowed($staff, 'user', 'manage')) { throw new \InvalidArgumentException("Can't prevent yourself from managing users"); } // set role if ($staff->getId() == NULL) { // add $role = new Role(); $em->persist($role); $staff->setRole($role); } parent::save($staff, $values); }
/** * Get collection by given criteria * * @param array $criteria * @param array $orderBy * @param int $limit * @param int $offset * @return Newscoop\PaginatedCollection */ public function getCollection(array $criteria, array $orderBy, $limit = null, $offset = null) { $qb = $this->repository->createQueryBuilder('u'); $qb->setFirstResult($offset); $qb->setMaxResults($limit); if (!empty($criteria['q'])) { $q = $qb->expr()->literal('%' . $criteria['q'] . '%'); $qb->andWhere($qb->expr()->orX($qb->expr()->like('u.username', $q), $qb->expr()->like('u.email', $q))); } if (!empty($criteria['groups'])) { $qb->join('u.groups', 'g', 'WITH', 'g.id = :group')->setParameter('group', $criteria['groups']); } if (isset($criteria['status'])) { $qb->andWhere('u.status = :status')->setParameter('status', $criteria['status']); } foreach ($orderBy as $column => $dir) { $qb->addOrderBy("u.{$column}", $dir); } return new PaginatedCollection($qb->getQuery()); }