How can I add a custom user variable for the user object? I want something that a user can customize in their profile page and that I can retrieve in the user object in another module, for example a preference for currency USD or EURO.
I would accept the simplest idea that doesn't involve writing a module, unless is no other way.
You can use the Profile module to define additional settings that users will be able to edit in their profile page and will be available in the $user object: http://drupal.org/documentation/modules/profile
Furthemore, you should read How to create new profile fields.
Related
I try to manage the access rights for users to edit or view different articles.
Articles can be created dynamically and the rights should be editable for every article.
In my case, I have a User object and multiple other objects (Article, and more...).
I need to check if a User can read or write any kind of object.
I actually see there is a method Voters, but they only can manage User groups?
Can somebody help me?
A Voter can decide almost anything - usually it's based on a user's permission, but it doesn't have to be - I've used one as a 'feature flag' check, with a value fetched from a configuration, or database entry to show something - or not, as an example.
The page on voters has an example on viewing, or editing a database record (a Post entity, via a $this->denyAccessUnlessGranted('edit', $post);.
In your instance, the voter would be passed the 'attribute', the object (Article, etc) you want to check on, and gets the current user from a service. If that user has the appropriate permission to read/edit/delete the Article or other object, it returns true.
I've created a User custom class in my Bundle. It inherits from the BaseUser class of FOSUserBundle.
In my class, I've defined several attributes which are entities of my Bundle, like Adress, Avis etc.
I have defined the formType of all my forms with data_class User.
It allows me to retrieve interesting user information like username (!) and displaying it in my forms.
BUT when I validate my forms it asks me to fill the plainPassword field of User class as it is a mandatory attribute.
First I wanted to retrieve the password from database to fill it in the form before displaying it but it seems impossible as a security measure.
So I've tried to stock it in the session (ugly I know) after registration but it seems not possible to force the form data with a value (surely because it is a password type field)...
So question is : what would you do ?
But what is the purpose of retrieving it?
You want to modify any of user's data like username or email?
Remember you can have /profile 'module' in FOSUser Bundle, which is used for modifing for example username and email.
For changing password you have separate 'module' change password (I don't remember path). Maybe that's what you are looking for? Of course that way user can edit only his own data. These modules are ready to use by deafault (you have to provide routing for them).
If you want for example, that admin modifies the other user's data that can be interesting for you:
In case of password, take a look at column in db called 'salt', which is used for encoding password in db. If you try to save password in db without salting it won't work - so I think if you want to change the password in db by some custom action - you have to set plain password and the it will be automatically encoded.
If you want to fill some form's fields by default read something about forms in Symfony, Fields types and something about their additional features and remember that password field require individuall approach.
I would not store the users plain password anywhere, let alone display it in the browser.
Playing with plain text passwords like you are doing is major security threat to your application, and any and all users using it.
The default User class in FOSUserBundle has two fields for the password, password and plainPassword. That plain password is filled in by the form, then, in the controller, the password field is generated by whatever encryption method you have configured for the firewall. The new user is then added to the database and the plain password field is cleared and never used again.
If you are trying to set up a forgot password solution, I would recommend emailing the user with some kind of unique key (as a URL parameter), ask them to confirm then give them the opportunity to update their password.
I'm trying to create/edit users and I'm having a slight problem with the password field.
When creating a user, it is fair to assume that the password field is required, however, I don't want it required when editing a user
Is there an easy way to do this?
I'm using the SonataAdminBundle to manage the User Form (NOT SonataUserBundle)
I think you can do it in two ways:
Use inheritance. Create a base FormType and then inherit it to create CreateUserType and EditUserType that add custom password settings
Use same form and leave password as not required, then in the "create user" controller check if password field has been filled and throw an exception if it is empty
Maybe there are more approaches to your problem. I usually use the second one but choose the one fits best to you.
I have a Drupal website that uses the Quiz module to administer tests to visitors. These tests need to be available to anonymous users. My problem is that I need to be able to ask the test taker to enter name and e-mail so the results can be sent to them. I just don't know how to go about doing this. I consider myself a beginner in Drupal and PHP.
Any help would be appreciated.
-First of all, create a custom module.
-Secondly, you need to add the email address and name fields. You can do this by either adding the two fields via hook_form_alter in your custom module or by enabling and using the Short Answer module/field that's included in your Quiz module and then customising the style of the field according to your needs (because it'll look like a question). Personally, I would recommend adding them using hook_form_alter. Plus, I suggest you learn about Hooks in Drupal, it will make your life easier.
-You have to validate and retrieve the values for the two fields. You can also use the same form_alter hook for this. Add a validation and a submission function to the validate and the submit stack of your Quiz form ($form['#validate'][] = 'your_validate_function'; and $form['#submit'][] = 'your_submit_function';). You can validate your email by using Drupal's function valid_email_address and, of course, you can validate other fields and calling Drupal's form_set_error to notify users of any input mistakes.
-To send your email after the Quiz is submitted, call Drupal's drupal_mail, in your submit function, which basically takes all the parameters needed to send an email. You'll have to create a hook_mail in your custom module. Check out an example of how to do this here. You can retrieve your form values (name and email address) from the local array $form_state['values'], pass them as $params to your drupal_mail function and add them to the body of your email in your hook_mail function. And that's it :D
-Alternatively, you can send an email by creating an action and assigning an action to be performed after a user has completed this Quiz. The Quiz module has support for that. Here's an example of how to write an action.
You may also use hook_quiz_finished instead of form submit callback.
Quiz module uses it to perform actions like sending results over email at the end of a quiz.
function mymodule_quiz_finished($quiz, $score, $session_data) {
//Sending e-mail.
}
If you ended here and you are using Drupal 7, go to Rules http://www.yourwebsite.com/admin/config/workflow/rules, and set the rule that the Quiz module has made available already to active under the settings, the rule is called "Send quiz results at the end of a quiz". I couldn't find the ability to do this anywhere within the Quiz config itself. Tested and its working. Be sure that the Rules Module UI is enabled to allow you to make the edits.
I have a PloneFormGen form with a Custom Script Adapter that, on form submission, generates a new uid and creates a folder with that uid as its ID.
from DateTime import DateTime
uid = str(DateTime().millis())
target.invokeFactory("Folder", id=uid, title=form['your-name'])
It uses input from the form to create various items in that folder, including a document called "newpage", which I then want to include a link to in an email. Is there a way to pass the uid variable from the script adapter to the mailer adapter, so that I can generate the link to the new documents inside the folder?
For instance, the script adapter creates a folder with the ID 1317142676351, so when the form is submitted, the URL for one of the new pages is http://mysite.com/submissions/1317142676351/newpage/. I want to be able to include that link in one of my Mailer Adapter templates, but I don't know how to get the uid that was generated in the Custom Script Adapter, or if that's even possible. What happens first, the Mailer, or the Custom Script Adapter?
Any push in the right direction is greatly appreciated!
Just copy the id value to the request. In the script, code like:
request.set('custom_id', id)
will do it. If you need the variable to be automatically handled, create a matching hidden variable in the form.