Taking Username Out Of Equation

11 years 11 months ago #202693 by timurdavidov
Taking Username Out Of Equation was created by timurdavidov
Hello all!

What's the most efficient way to taking username out of equation: both in registration and further use of the website on the front-end?

I am currently hiding username during registration and using email to log in to the front-end. The problem that I encountered is that CB auto-generates usernames from the members' names. This means that another person with the same name cannot register.

Possible solutions to that are:

1. Auto-generate a unique username from the e-mail address;

2. Auto-generate a unique username from first + last name + random number.

Please help me solve this dilemma.

Thanks a lot in advance.

Please Log in to join the conversation.

11 years 11 months ago #202708 by ducks_mrd
Replied by ducks_mrd on topic Re: Taking Username Out Of Equation
Hi timurdavidov,

Firstly thanks for spotting this, I to am not using usernames but had not spotted this issue. I am just about to go live and this could have been a bit embarrassing.

Anyway I have now got my site registration by using the email address as the username. I only accept unique emails so there will be no duplicates.

This took a little finding to follow the registration flow but to solve this I changed:

components/com_comprofiler/library/cb/cb.tables.php
Line 1379
$this->username = $this->name;
to
$this->username = $this->email; //default $this->name

I also have the cb_connect plugin which uses the username from the external site (eg. Facebook) as the new username. This may cause duplicates so I also changed.

components/com_comprofiler/plugin/user/plug_cbconnect/cb_connect.class.php
Line 618
$username	= $connect_user->name;
to
$username = $connect_user->email; //default $connect_user->name

These changes got everything working again for me.
But you might want to wait until one of the Admins has confirmed if these changes are okay and do not break anything else or if I have missed something.

Kind Regards

Mike

Please Log in to join the conversation.

11 years 11 months ago - 11 years 11 months ago #202749 by pepperstreet
Replied by pepperstreet on topic Re: Taking Username Out Of Equation

timurdavidov wrote: ... 2. Auto-generate a unique username from first + last name + random number...


Here are some interesting posts with similar requests and valuable information:

Autogenerate random username

Auto-generate numeric username

There might be other ones...

In general, you should have a look at CB-Auto-Actions in Incubator plugins.
The following user(s) said Thank You: krileon

Please Log in to join the conversation.

11 years 11 months ago #202795 by krileon
Replied by krileon on topic Re: Taking Username Out Of Equation
CB Auto Actions using the after registration trigger is best approach. Depending on how complex you need the username to be you could just use the Field action otherwise you'd want to use a Query action to query the _users database table and alter the username. I do not recommend core edits as suggested by ducks_mrd. Please understand by implementing a core it it basically voids support and once discovered we will be reluctant to help you if an issue should arise until the core edits were reversed (CB re-install over it self).


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

11 years 11 months ago #202826 by timurdavidov
Replied by timurdavidov on topic Re: Taking Username Out Of Equation
Thanks, Krileon! I am all for avoiding hacks, especially when there is a plugin that can do the job.

By the way, where can I find documentation for Auto Action plugin?

Please Log in to join the conversation.

11 years 11 months ago #202851 by ducks_mrd
Replied by ducks_mrd on topic Re: Taking Username Out Of Equation
Hi Kyle,

Apologies for giving a hack as my response to the question however having spent some time on solving this I could not get Auto Actions to work on this occasion to solve this problem.

I have this evening again looked at the code and found why I could not get it to work with Auto Actions, this is something you may want to have a look at.

Apologies for this long explanation.

I like, timurdavidov, am not wanting to use the username field, however CB always requires that one is set.
To disable the use of the username on the field page of the CB setup I changed the username field settings to:
Required - No
Profile - No
Registration - No
Searchable - No

Now that part works fine. (I have a separate request/issue though I will ask later).

With these settings on registration the username is created either from the name field or if you do use name field then the email.

Like timurdavidov this is where we get the username warning as two people with the same name can't be registered as it creates a duplicate username.

So I looked at Auto Actions to help, what I found was:

Trigger onAfterUserRegistration is too far along the process you still receive the error when registering as this trigger is done at the end once the data is in the tables.

I then tried the onStartSaveUserRegistration trigger which does get fired before any data is entered.
The field action type wont work here as nothing has been set yet we are still working with the $_POST data.
So I tried using the code type and method of PHP and my code was:
$_POST['username'] = 'test';
This did not work I still kept getting the username warning. Even though the saveRegistration function in comprofiler.php uses the raw $_POST variable.

This is where I cheated I just did the core hack rather than figure out why that simple auto action did not work.

I have now gone back and had look at why this was not working.

So I followed your functions through the registration process.
I will mention everything I noticed.

comprofiler.php
line 959 saveRegistration function starts

at line 960 the $_POST is brought it and doing a print here I can see that my form submission $_POST is empty.
line 984 
$_PLUGINS->trigger( 'onStartSaveUserRegistration', array() );
auto action triggered and $_POST = 'test' as set in my auto action.
line 1042
$saveResult = $userComplete->saveSafely( $_POST, $_CB_framework->getUi(), 'register' );
my update $_POST is used here but it was returning false so the javascript alert is show 2 lines below at 1044.

looking at the saveSafely function.
administrator/components/com_comprofiler/library/cb/cb.tables.php starting line 1350
function saveSafely( &$array, $ui, $reason ) {
this passes the $_POST in as the variable $array.
$array is still correctly set as "test".
line 1365
$bindResults = $this->bindSafely( $array, $ui, $reason, $oldUserComplete );

this accepts the $array variable with my username but outputs to $bindResults with the username set as empty.

As the username has now been unset, starting at line 1367 the username is set to the name field. This is the bit I hacked to be the email instead

Up until $this->bindSafely everything was okay but it unsets the username.

the bindSafely function starts line 864
doing a print shows $array is still set with the new username.

lines 928 to 936 is where the $this variable which is at this point just has empty fields in it gets populated with the processed fields and the username becomes empty.

Now I can keeping drilling down in to the code

But I noticed at this point if in the field settings for the username field you select yes to show on registration.
Then the username field is not unset and stays as the value I set in my Auto Action.

So wherever the code goes next between 928 and 936 when it populates the $this variable. There must be a check being done that if we are not showing the username then unset it and let the code as described above change it to the name or email.

I am just guessing from trial and error that the code between 928 and 936 is looking at the user plugins in components/com_comprofiler/plugin/user and in particular the plug_cbcore but I can't get my head around where it unsets it as the file is quite large.

To cut a long story short
To make this work with auto actions and not have to use the hack then it must not unset the username.
Now I guess this unset is done by design so if the site is not using usernames a user can not inject their own username in to the original $_POST and have it set.

As a suggestion instead of the unset being so far down in the registration process bring it right to the start of saveRegistration. If the username is set in the $_POST unset it then let the auto action at onStartSaveUserRegistration set it to whatever.

Apologies again for the long reply but I thought it better to explain what I had found rather than being unhelpful and just saying "it does not work".

Kind Regards

Mike

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.298 seconds

Facebook Twitter LinkedIn