4

Is there a way to prevent the current user from changing fields in his profile? Currently i've attached a customfield "company" to the user object. I only want the site admin to set this field once and the only one allowed to change this. Where can I inject/override/overload the behavior of the saveUser method? Or is there a way to allow a different validation function or to disable a certain customfield during edit/save?

Thanks in advance.... 

  • Hey pythic were you able to solve this issue? I am having a similar problem and could use some help. – Moin Qidwai Oct 16 '14 at 11:01
  • Moin, its still on my todo list. I'll keep you posted once this is fixed. If you have a solution let me know... Thanks – pythic Oct 18 '14 at 12:02
3

There isn't anything to do this out-of-the-box in Craft.

I believe the proper way you'd want to approach it would be to write a plugin that has it's own field type that takes into account user permissions.

An admin would get a normal text input, for example, and any other user would just see read-only text.

2

I did not have a lot of time to find a solution for this so I used a workaround maybe that can work for your case as well.

So firstly my case was such that I needed to have an entries field type that only the admin can edit for each user. It was NOT important in my case for the user (For whom the field is being edited) to see which entries are attached to his field in the backend control panel. I could show it to them in the front end. Hence what I did was I modified the entries fieldtype form template (Which is located at craft/app/templates/_includes/forms/elementSelect.html) and added two if statements. Firstly I added an if statement to check the name of the field and then another if to show or hide the add entry button. Hence if my entry name is userSubscriptions then the html looked as follows:

{% if name == 'userSubscriptions' %}
    {% if currentUser == 'admin' %}
        <div class="btn add icon dashed">{{ addButtonLabel }}</div>
    {% endif %}
{% else %}
    <div class="btn add icon dashed">{{ addButtonLabel }}</div>
{% endif %}

But obviously this is not the best way of doing it. Its just a temporary solution if you really need to do it fast. Im not sure if this could work with your fieldtype as well but thats my take on it. I will keep trying to find the correct way of doing this and if I manage to do it Ill let you know and if you manage to do it the other way then let me know how you did it. Thanks

2

You could use disabled to prevent people from filling it except the admin:

    {% if currentUser == 'admin' %}
      <input type="text" name="email" value="{{currentUser.username}}">
    {% else %}
      <input type="text" name="email" value="{{currentUser.username}}" disabled>
    {% endif %}

The field still get's filled but users are prevented from writing anything on it. Hope this is what you were after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.