Re,

On Mon, Jun 15, 2009 at 5:21 PM, Guillaume Lerouge <guilla...@xwiki.com>wrote:

> Re,
>
> On Mon, Jun 15, 2009 at 5:09 PM, Sergiu Dumitriu <ser...@xwiki.com> wrote:
>
>> Guillaume Lerouge wrote:
>> >>>
>> >>> How exactly are you adding the user to that group? If you create an
>> >>> XWikiGroups object, add it to the group document, then save it, you
>> >>> should use saveWithProgrammingRights() instead of save().
>> >>>
>> >>>
>> >> THis is from the NoAccess.WebHome page.
>> >>
>> >> Here is the velocity code segment I am using to try and add a user to
>> the
>> >> TrainedUsersGroup group.
>> >>
>> >> 01 #if($completed_training == "true" or $xwiki.hasAdminRights())
>> >> 02   ##
>> >> 03   ## Add the current user to the TrainedUsersGroup group
>> >> 04   ##
>> >> 05   #set($MyGroupDoc = $xwiki.getDocument("XWiki.TrainedUsersGroup"))
>> >> 06   #if(!$MyGroupDoc.getObject("XWiki.XWikiGroups", "member",
>> $fullName))
>> >>
>> >> 07     #set($GroupObj = $MyGroupDoc.newObject("XWiki.XWikiGroups"))
>> >> 08     #set($AddingUser = 1)
>> >> 09     $GroupObj.set("member", $fullName)
>> >> 10     $MyGroupDoc.save()
>> >> 11   #end
>> >> 12 #end
>> >>
>> >>
>> >
>> > To do so, you can go to
>> > .../xwiki/bin/edit/XWiki/TrainedUsersGroup?editor=rights and make sure
>> that
>> > the "Edit" checkbox is ticked in the XWikiAllGroup line. This allows all
>> > users to modify that page, thus making them able to add themselves to
>> the
>> > group.
>> >
>> > Once I had made the modification, my "normal" user was able to add
>> himself
>> > to the TrainedUsersGroup simply by going to the page.
>> >
>> > Hope this helps,
>>
>>
>> Guillaume, stop confusing.
>>
>> Dean, as I said, replace the save() call with saveWithProgrammingRights().
>>
>> 10     $MyGroupDoc.saveWithProgramminRights()
>
>
> Well... As Dean stated it in his email this didn't seem to help:
>
> "I've even changed line #10 to use
> saveWithProgrammingRights but it behaves the same and just displays on the
> screen in the case of a regular user."
>
> Guillaume
>
>
>>
>> --
>> Sergiu Dumitriu
>> http://purl.org/net/sergiu/
>> _______________________________________________
>> users mailing list
>> users@xwiki.org
>> http://lists.xwiki.org/mailman/listinfo/users
>>
>
>
>
> --
> Guillaume Lerouge
> Product Manager - XWiki
> Skype: wikibc
> Twitter: glerouge
> http://guillaumelerouge.com/
>

I've done further testing, here goes:
I've inputted the following content in a page:

<%
def completed_training = "true"
%>

#if($completed_training == "true" or $xwiki.hasAdminRights())
##
## Add the current user to the TrainedUsersGroup group
##
#set($MyGroupDoc = $xwiki.getDocument("XWiki.TrainedUsersGroup"))
#if(!$MyGroupDoc.getObject("XWiki.XWikiGroups", "member", $context.user))

#set($GroupObj = $MyGroupDoc.newObject("XWiki.XWikiGroups"))
##set($AddingUser = 1)
$GroupObj.set("member", $context.user)
$MyGroupDoc.saveWithProgrammingRights()
#end
#end

It works only if the user looking the page is an Admin. If the user is not
in the AdminGroup, he doesn't get added to the group. If I remove
"or $xwiki.hasAdminRights()" from the #if it no longer works, in any case.
Thus I suspect that the "true" value returned by the groovy call isn't
interpreted as it should by the velocity check in the #if call.

For instance, if you try to use only the following code in a page:

<%
def completed_training = "true"
%>

* First: $completed_training

#if($completed_training == "true")
* Second: $completed_training
#end

The First line works fine while the Second never gets displayed. I'm not
sure if that's your exact use case (a variable defined in groovy on the page
that you try to call from velocity on the same page), but if it is, then for
some reason it doesn't work. Here's a workaround that seems to work (though
it requires that the user clicks on a link to add himself to the group):

#if(!$request.completed_training == "true")
#set($MyGroupDoc = $xwiki.getDocument("XWiki.TrainedUsersGroup"))
  #if(!$MyGroupDoc.getObject("XWiki.XWikiGroups", "member", $context.user))
    #set($GroupObj = $MyGroupDoc.newObject("XWiki.XWikiGroups"))
      $GroupObj.set("member", $context.user)
      $MyGroupDoc.save()
  #end
#else
[Click here to confirm that you have finished the
Training>NoAccess.WebHome?completed_training=$completed_training]
#end

Good luck,

Guillaume

-- 
Guillaume Lerouge
Product Manager - XWiki
Skype: wikibc
Twitter: glerouge
http://guillaumelerouge.com/
_______________________________________________
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users

Reply via email to