On 06/24/2010 12:10 AM, xManish wrote:
Fabio Mancinelli-4 wrote:
So as long as what you want to do is achievable by page and object
manipulation (like the most of the XWiki functionalities) you are able
to use the REST api to do it.
Hi Fabio,
Thanks for replying to my thread.
Coming to the creating user issue. Does that mean we can create a user by
creating a page and have object of class XWikiUser, but won't be able to add
the user details like personal information and contact information through
REST Api?
Or may be there is a way to do it??
Thanks,
Manish
Just to be clear I'll show you how to create a user using curl
(
http://curl.haxx.se), a handy command line tool for sending HTTP requests.
Step1: Create a page that will represent the user. This page will be
located in the XWiki space and its name will correspond to the username
$ curl -v -u Admin:admin -X PUT -H "Content-type: text/plain"
--data-ascii '{{include document="XWiki.XWikiUserSheet"/}}'
http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/Foo
Quick explanation:
* -u Admin:admin uses HTTP basic authentication in order to authenticate
the request. It will be executed as Admin.
* We send PUT request
* The content type is text/plain. This is accepted by the
http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/Foo
resource and the effect is that whatever is specified as the request
payload (entity) will become the page content. You could have also used
the application/xml content type, but in this case you should have sent
an XML document that is compliant to the REST Api model schema for Page
objects defined here:
http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-rest/src/main/…
Once this command is executed you will receive back the XML
representation of the newly created page and if you point your browser
to
http://localhost:8080/xwiki/bin/view/XWiki/Foo you will see a user
page that is incomplete; in fact the message says: "This stylesheet must
be applied on a document containing a XWiki.XWikiUsers object."
Step2: Create a XWiki.XWikiUsers object containing user information.
$ curl -v -u Admin:admin -X POST -d
"className=XWiki.XWikiUsers&property#first_name=Foo&property#last_name=Foo&property#password=foo"
http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/Foo/objects
Quick explanation:
* We create a new XWiki.XWikiUsers object attached to the previously
created page. This time the content type will be
"application/x-www-form-urlencoded" sent by default by curl.
* The entity will contain several urlencoded fields that will be used to
initialize the fields of the XWikiUsers object. In particular these
fields are:
** className : Tells the REST Api that we want to create an object of a
given class, in this clas XWiki.XWikiUsers
** property#first_name, property#last_name, property#password : These
are used to define the values that will be set to the object's property.
The property# prefix is not part of the property name but it is used to
distinguish metadata about the object (like the classname) from the
actual object's properties. To check the available properties for the
XWiki.XWikiUsers class you can look at
http://localhost:8080/xwiki/bin/view/XWiki/XWikiUsers. It is *here* that
you specify all the data associated to your user: her name, last name,
blog, password, etc.
Once this command is executed you will receive back the XML
representation of the newly created object and if you point your browser
to
http://localhost:8080/xwiki/bin/view/XWiki/Foo now you will see a
complete user page with all the correct details.
Step3: Add the newly created user to the XWikiAllGroup in order to make
it correctly access the wiki.
$ curl -v -u Admin:admin -X POST -d
"className=XWiki.XWikiGroups&property#member=XWiki.Foo"
http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/XWikiAllGro…
This should be pretty clear now. You just add a XWiki.XWikiGroups object
to the XWiki.XWikiAllGroups page and set the member field of this object
the user id, in this case XWiki.Foo
Now you should be able to login to the wiki using the new user (i.e.,
Foo, password foo).
Actually there is another step to be done before Step3. In fact, if you
login as Foo and you go to the Foo profile you will see that you won't
be able to edit it. This is because you need to set the access rights to
this page in order to make Foo able to edit it.
I leave this as an exercise. (Hint: look to the XWiki.XWikiRights
objects attached to a user created in the standard way)
Hope this helps.
-Fabio