Plugins | sfDoctrineGuardPlugin - set a group to user from frontend - symfony-1.4

I need to set a Group to a user in frontend application. Here what i have done.
$g = sfGuardGroupTable::getInstance()->findOneById(1);
$u = new sfGuardUser();
$u->setFirstName($request->getParameter('first_name'));
$u->setLastName($request->getParameter('last_name'));
$u->setEmailAddress($request->getParameter('email_address'));
$u->setSfGuardUserGroup($g);
$u->save();
The user is inserted successfully but I cannot set a group to a user.

There's a many to many relation between sfGuardUser and sfGuardGroup, therefore each sfGuardUser has a collection of sfGuardGroup objects. You have to get the collection and add group to it:
$u->getSfGuardUserGroup()->add($g);

Related

Entity data in crudcontroller based on current logged in user or user id

I'm new to Symfony and I've already learned a lot, but now I run into a problem that I can't find the answer to.
I have a crudcontroller which uses the entity (e.g.) Articles, but the articles in the database have different user ids.
The crudcontroler MUST only show the articles of the current logged in user/user id. I know how to get the user id, but I don't know how to manipulate the entity to ONLY get the articles for the specific user to display in the crudcontroller.
Example:
User with user id 5 is logged in the dashboard. Get articles where user id is 5 in crudcontroller ONLY. Do not get or display any other.
Symfony version: 5.2
Easyadmin: 3.2
I hope someone can push me in the right direction. Currently pulling my hair out on this one. Nothing to be found on Google on this subject.
EDIT:
I've finally found the way to have more control with the createIndexQueryBuilder().
I've overridden that one with:
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$response = parent::createIndexQueryBuilder($searchDto,$entityDto,$fields,$filters);
$response->where('entity.user_id = 5);
return $response;
}
BUT then I get this error:
[Semantical Error] line 0, col 65 near 'user_id = 5': Error: Class App\Entity\RepairServices has no field or association named user_id
That's right because user_id isn't part of the entity BUT it is in the database and I need to check on it. How can I do that?

Symfony/SonataAdmin Show fields from two entities

I have two entities :
User that contains login data (with FOSUser),
and other informations about them (name, first name, date of birth, etc) in another entity called UserInfo.
In SonataAdmin, I want to manage my Users (done) but I need to add fields in the table that are in UserInfo (name, first name...).
Any idea ?
Thanks !
Depending on the relationship type, you should be able to just reference userinfo.firstName, eg:
public function configureShowFields(ShowMapper $show)
{
$show->add('userinfo.firstName')
->add('userinfo.dob');
}
Of course, if you have many userinfo's attached to the entity I don't think this will work.
There must be existing a relation in between user and userInfo. say OneToOne relation. Then from userInfo entity u will get the user data and will show on the Admin side.
i.e.
$subject = $this->getSubject();
$user = $this->subject->getUser();
Will give you user, if you want to further perform actions on that user.

pagerfanta - DoctrineORMAdapter Sorting

Although I know this is trivial I'm stuck trying to implement the pagerfanta Paginator using the DoctrineORMAdapter, I want to paginate all entities sorted by id in descending order, the final SQL I want is this:
SELECT id, name FROM User ORDER BY id DESC LIMIT 0, 5;
Suppose I had users from A to Z and I want to limit them by 5 each page, what DoctrineORMAdapter is paginating results in User E to A listed in the first page, while what I actually expect is to see User Z to user V in the first page, U to Q in the second page and so on. The DQL I'm passing to DoctrineORMAdapter is as follow:
SELECT u FROM My\FluffyBundle\Entity\User u ORDER BY u.id DESC
On execution this is the final DQL for the first page:
SELECT DISTINCT id0 FROM (SELECT u0_.id AS id0, u0_.name AS name1 FROM User u0_
ORDER BY u0_.id DESC) dctrn_result LIMIT 5 OFFSET 0
Please note that when using the ArrayAdapter instead of DoctrineORM's it works as expected, but it's not a good idea to rely on ArrayAdapter when you have thousands of complex Doctrine Entities, not even with extra lazy loading :D.
This is the only relevant code:
$queryBuilder = $repo->createQueryBuilder('u')->orderBy('u.id', 'DESC');
$adapter = new DoctrineORMAdapter($queryBuilder);
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(5);
Thanks.
This will help you:
$adapter = new DoctrineORMAdapter($queryBuilder, false);
Had the same problem this morning.
By default Pagerfanta is treating your query as one with joins. Setting second argument to false makes it use simple query handling.
In Kunstmaan Bundle, in AdminListConfiguration class, you have to overide function that is creating Pagerfanta, if you want to sort simple entity.

How to: Unpublish Nodes by Author when Author is assigned Role

I'm using Drupal 7 + Rules. I would like to create a rule that unpublishes all nodes authored by a user when they have been given a particular role.
EVENT - After updating an existing user account
CONDITION - User has role(s): SelectedRole
ACTION - ???
BONUS: If this could be limited to nodes of a certain type, that would be even better.
If there is a better way outside of Rules to do this, I'm open to other ideas.
Thanks so much!
You can create a custom ruleset to loop through the nodes or a Views Bulk Operation action.
An easiest option is to add a custom PHP function on your rule (PHP > Execute custom PHP code). Of course you have to enable the php filter core module if you didn't already.
In the PHP action you have to get all the nids of the published nodes the current user and loop through them to unpublish them. I will use the EntityFieldQuery API class but you can use the database functions also.
// Get updated user id
$uid = $account -> uid;
// Get all nodes from user that are of NODE_TYPE and are published
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'NODE_TYPE')
->propertyCondition('status', 1)
->propertyCondition('uid', $uid);
$result = $query->execute();
$nids = array_keys($result['node']);
// Load all nodes in one go for better performance.
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
// set status property to 0 (unpublished)
$node->status = 0;
// re-save the node
node_save($node);
}
I would also suggest to add one more Condition for the user before the one you are using: User Has Roles: (NOT) SelectedRole so that the action do not run everytime the user profile is updated.
References:
Use Rules to Unpublish content based on Author's role
Publish unpublished node programmatically
Find nodes created by user (more programmatically)

Change node author automatically

Anonymous user is able to post nodes. After posting node, user is redirected to registration. After registration, the previously submitted node should be linked with newly registered user.
I played with rules and entities but I was not able to get it work properly. Any ideas?
I would write a custom module (but that's me). The module needs to implement hook_node_insert and save the nid into SESSION. Then on hook_user_insert it can do the change. Untested code:
function foo_node_insert($node) {
$_SESSION['mynodes'][] = $node->nid;
}
function foo_user_insert($edit, $account) {
if (!empty($_SESSION['mynodes'])) {
foreach ($_SESSION['mynodes'] as $nid) {
$node = node_load($nid);
$node->uid = $account->uid;
// This saves the revision as the current user uid but that's just what we wanted.
node_save($node);
}
}
}
Edit: don't forget unset($_SESSION['mynodes']);
Save the node data until after registration and post it then.
There's the Anonymous Node Create module.
The module allows anonymous users to create nodes. But 'anonymous' is questionable in this module. This module alters the node form for anonymous users by adding two field groups at the end before the save button.
The first field group has fields that allow users to create a new account. This new account is then the author of the new node created.

Resources