[How To] Create a custom identity service

Status
Not open for further replies.

Spartan

OMG Member
Joined
Jul 10, 2008
Messages
337
XenForo comes with numerous Identity Services built in such as Windows Live and Skype but sometimes you may wish to let your members add others.

In this tutorial I will show you how to create a custom Identity Service to allow your members to add their XenForo Community user name to their profile.

Step 1

Before you can add a new Identity Service in the admin control panel, you will need to create a php file in library > XenForo > Model > IdentityService. You can name it whatever you wish but to keep things simple and so I can easily see what the file does, I named mine xenforo.php

In xenforo.php, you will need to add the following code.

PHP:
<?php

class XenForo_Model_IdentityService_xenforo extends XenForo_Model_IdentityService_Abstract
{
    protected function _getIdentityServiceId()
    {
        return 'xenforo';
    }

    static public function verifyAccountName(&$accountName, &$error)
    {
        return true;
    }
}

Step 2

Now the php file is created, we can go into the admin control panel and add a custom identity service which is located at Admin Control Panel > Users > Identity Services which will look like this;

screen1.png


Click on Add New Identity Service which will then give you this;

screen2.png


Now you have to enter the details for this identity service, you can use any thing in these fields except the PHP Model Class Name which must be the same as stated in the php file.

Service ID: xenforo
PHP Model Class Name: XenForo_Model_IdentityService_xenforo
Service Name: XenForo Community
Account Name Description: Your user name

screen3.png

Click Save Identity Service and now your done.

Now your users will see an extra option on their contact settings page.

screen4.png

Once they enter their details, their profile page will look like this.

screen5.png

And there you have it, a working customer identity service.

I hope this little how to is helpful to you, feel free to ask questions if you're unsure of anything.

--Doctor
 

Mikey

:mikey:
Staff member
Joined
Jan 26, 2008
Messages
17,836
Nice! Can anyone explain why the PHP file is needed, as opposed to the vBulletin and IPB methods of just creating the custom field and hitting save? Am I wrong in comparing this to something like a custom profile field in vBulletin and IPB?
 

Spartan

OMG Member
Joined
Jul 10, 2008
Messages
337
Nice! Can anyone explain why the PHP file is needed, as opposed to the vBulletin and IPB methods of just creating the custom field and hitting save? Am I wrong in comparing this to something like a custom profile field in vBulletin and IPB?
No I don't think it's meant to be like the custom profile fields in ther others.

I believe, all though I could be wrong, that the php file is there to make sure the data entered is valid for the service.

For example, Twitter doesn't allow you do use a period in your name, if you try to enter a period in the Twitter field you get this warning;

Twitter:
Please enter a valid Twitter name, using only a-z, 0-9, and _ characters.

There could be other funtions to it but that's the only use I can see for it.
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,101
Oh wow, you learned that rather quickly. And thank you so much for sharing this on our site. I really appreciate that.
I will certainly be learning this and using it.
The ability to have custom behaviors specific for that identity service making this a very powerful tool.
 

TilkiBey

OMG Member
Joined
Jan 18, 2011
Messages
76
Nice! Can anyone explain why the PHP file is needed, as opposed to the vBulletin and IPB methods of just creating the custom field and hitting save? Am I wrong in comparing this to something like a custom profile field in vBulletin and IPB?
i am seeing this thread and this message now =)
so, sorry for this late answer

php file is needed for using preg_match options
example from facebook.php file
PHP:
if (preg_match('#^https?://www\.facebook\.com/(\#!/)?profile\.php\?id=(?P<id>\d+)#i', $accountName, $match))
		{
			$accountName = $match['id'];
		}
		else if (preg_match('#^https?://www\.facebook\.com/(\#!/)?(?P<id>[a-z0-9\.]+)#i', $accountName, $match))
		{
			if (substr($match['id'], -4) != '.php')
			{
				$accountName = $match['id'];
			}
		}
with these codes, you dont have to enter just username or just userid (your facebook account's)
you can enter
Code:
http://www.facebook.com/username
to facebook account area
this codes will change your defined facebook account name to "username"

and, after these lines, u can see these codes
PHP:
if (!preg_match('/^[a-z0-9\.]+$/i', $accountName))
		{
			$error = new XenForo_Phrase('please_enter_valid_facebook_username_using_alphanumeric_dot_numbers');
			return false;
		}
this codes is needed for checkup account name's chars (as limited characters,example from Turkish= ığüşöç)

at first code the $accountName is re-defined from URL
at second code the NEW $accountName is check-up for characters

so, you need a callback file for adding new identity =)
 
Status
Not open for further replies.
Top