Will it be possible for tokens to hold the users Role?

CommentFileSizeAuthor
#5 token_user.inc_.patch1.11 KBricflomag

Comments

greggles’s picture

This is certainly possible

One problem is that a user can have multiple roles. Should the [role] token return 1) all roles separated by commas, 2) the role with the lowest/highest rid value in the database 3) the role with the lowest/highest "name".

So, we'll need the proposal to handle these kinds of problems

Perhaps we need lots of tokens:

[role-name] - the name with highest rid value
[role-names] - ", " delimited list of roles like "anonymous", "authenticated", "admin"
[rid] - the highest rid for this user
[rids] - comma delimited list of rids

That could be overly heavy/complex, but we need some specific rules so we will return a consistent value.

moshe weitzman’s picture

perhaps let role_weight module expose the new tokens. it knows which role is most important for a given user.

amitaibu’s picture

Hi,
Moshe in my opinion Role weights is not the correct thing, I'll explain by my own example. In my site I have a designer, photographer, writer, etc;. There isn't really a higher role and lower one - just different tasks, thus different access.

As a remark, here is a another possible use for this feature. As a non-developer, this is so far the easiest way I've though in order to accomplish the redirecting task.

ricflomag’s picture

Assigned: Unassigned » ricflomag
Status: Active » Needs review

Here is a patch against token_user.inc to add two tokens for user roles :

  • ['role-names'] is a comma separated list of names
  • ['roles-ids'] is a comma separated list of rids

The problem is that any character can be used in the name of a role, so if a name includes a comma, there is a confusion. I don't know if it would be appropriate to escape them in the token list.

I use this patch with the module workflow_ng that provides a very handy REGEX feature that makes it easy to match a given role in the list.

Hope it helps.

ricflomag’s picture

StatusFileSize
new1.11 KB

I've forgotten to attach the patch...

ricflomag’s picture

Please read ['roles-names'] instead of ['role-names'].

greggles’s picture

Status: Needs review » Needs work

While I like the implementation and appreciate your providing this code, the issue is that we need agreement on a good way to implement this.

As you point out, your implementation makes it relatively difficult to deal with the result because it's impossible to know if the , is from the separation of roles or the name of the role.

I'm marking this as code needs work not because I'd like to see some more work in the code, but really because I'd like to see some discussion about token should handle tokens which are likely to return multiple values.

menupath and catpath are two existing tokens which probably do the wrong thing (delimit with / characters) but since they are used by pathauto that's considered acceptable.

Perhaps the answer is that consuming modules should have the ability to specify what they want as the delimiter. I welcome input of ideas on the subject.

amitaibu’s picture

Hi,
I think that this concept is a valid one, in the sense that a typical user would not create a role name with a comma. It's possible for him to do so, but in that rare case I would recommend this user to use the ['roles-ids'].
Having a simple solution that serves 99% of the use cases is better IMO.

ricflomag’s picture

In my particular case, i have a bunch of roles of the type : "Author of X", "Author of Y", "Editor of X", "Editor of Y" and so on. No matter the delimiter, i use workflow_ng REGEX feature to know whether the user is an author or an editor, or whether he belongs to group X or Y (he may have several roles).

But we can imagine a module that need to loop on each role name, which raises the question of being able to separate the names with the security of not cutting some of them. The token module could have a parameter called, say, "Token lists delimiter", which would default to a comma or a slash, and which would be available through a simple API call to other modules. This way, the administrator of the site could use any character in the names of the roles, and configure a special, unused character as a separator in token lists.

But as Amitaibu says, using rids instead of names is a simpler way to retrieve the user's roles if we need to be sure to separate each role from another. And then, get the names from their ids.

fago’s picture

although the order of roles can't be controlled, I think the posted patch would already be a useful addition.

Anyway, what token could really need is proper (unique) way for handling multiple values.

greggles’s picture

fago - I agree. Now, do you have a proposal for that way? ;)

fago’s picture

Yes I have! Sry, that it took that long, but here it is:

http://groups.drupal.org/node/5744

greggles’s picture

Status: Needs work » Closed (won't fix)

I created an issue for this in role weights since that would be the best place to do it #318777: Provide tokens for the highest weighted role.

I think that this issue should be "won't fixed" in favor of that one. If someone has an idea of how this can be usefully implemented without knowing weights, I'm all ears, but I don't see how the current code is all that useful.

pfaocle’s picture

I've provided a patch over on that issue implementing tokens for "heaviest" and "lightest" role ids/names. Anyone who requested this functionality is encouraged to test it and I'll commit and roll Role Weights 5.x-1.3. Thanks!

wickwood’s picture

Thanks for the patch in (#5), it helped me solve a problem I was having with the Mailchimp Module: #539136: Role as a Merge Variable (#7)

If there's a better way to do this now, I would appreciate being pointed in that direction or giving some suggestions. But this was the best solution I could find today. So I hope the solution to this problem isn't completely abandoned and the status can be changed from "won't fix" to "needs work". ;-)

All the Best,
Steve