The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Unique Account Usernames

Discussion in 'cPanel Developers' started by seengee, Aug 16, 2010.

  1. seengee

    seengee Member

    Joined:
    May 14, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Hi All,

    What methods do people use for guaranteeing the creation of a unique account username when using a createacct API call. I know in reality its possible to check the statusmsg for an error and respond to this with another API call if the account has failed on the account username not being unique but this seems like far too much effort. Really not sure why either the account username isnt just created dynamically as a unique username or why there isnt an API call to get a unique account username (unless there is and i've missed it somehow).

    Thanks
     
  2. vanessa

    vanessa Well-Known Member
    PartnerNOC

    Joined:
    Sep 26, 2006
    Messages:
    817
    Likes Received:
    22
    Trophy Points:
    18
    Location:
    Virginia Beach, VA
    cPanel Access Level:
    DataCenter Provider
    generally when you reach the point that you're worried about duplicate usernames, you have a more sophisticated method of assigning them. For instance, whatever billing or customer interface you're using will assign a username based on the domain, and if that username already exists, a new username is assigned. In other words, the billing system assigns the username before the API call to WHM is used.
     
  3. seengee

    seengee Member

    Joined:
    May 14, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    I'm not sure i agree with you to be honest. I can't really think of a use-case where an external username would want to be used within cPanel/WHM. Combined with that is the fact that WHM itself expects the admin user to determine that the username is unique when adding an account, this doesnt seem very elegant to me at all. Its very commonplace now in signup forms to have something along the lines of "check availability" for usernames. It would seem very useful to make this possible within WHM and then publish this element of the API.
     
  4. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi Seengee,

    I see your point: Such a mechanism would be feature request. You're welcome to post your idea in the Feature Request forum.

    I think there's some validity to what Vanessa is saying, though. For instance: if you had many, many WHM/cPanel boxes you'd probably used a centralized billing system for all the servers, otherwise keeping track of accounts on each server would be an overwhelming task. And depending on your desire or the billing software, it may be a requirement in the workflow that all your accounts had unique username across the entirety of your farm, not just any given server. It only makes sense then to use the billing system to automate account creation and management. In that scenario, you billing system would have the logic to keep track of account details; enough so to answer a 'check availability' type question. The billing system would use the APIs to perform CRUD as necessary on the server farm.

    -OR- you could roll the feature yourself with some:
    If you had only a handful of servers, you could make a script that aggregates a username list. I say only a handful cause this solution my not scale well, depending on your particular implementation.

    You'd use the XML-API, the function 'listaccts'.

    The query URL is simple:
    Code:
    https://10.1.1.1:2087/xml-api/listaccts
    
    it would generate a response something like:
    Code:
    <listaccts>
      <acct>
        <disklimit>unlimited</disklimit>
        <diskused>0M</diskused>
        <domain>dave.com</domain>
        <email>dave@test.test</email>
        <ip>192.168.99.190</ip>
        <maxaddons>*unknown*</maxaddons>
        <maxftp>unlimited</maxftp>
        <maxlst>unlimited</maxlst>
        <maxparked>*unknown*</maxparked>
        <maxpop>unlimited</maxpop>
        <maxsql>unlimited</maxsql>
        <maxsub>unlimited</maxsub>
        <owner>root</owner>
        <partition>home</partition>
        <plan>default</plan>
        <shell>/bin/bash</shell>
        <startdate>10 Aug 10 15:03</startdate>
        <suspended>0</suspended>
        <suspendreason>not suspended</suspendreason>
        <suspendtime></suspendtime>
        <theme>x3</theme>
        <unix_startdate>1281470609</unix_startdate>
        <user>dave</user>
      </acct>
    </listaccts>
    
    If you use the XML-API PHP client class, you code would look like this, only you'd have to loop/aggregate the results for the master list of what is not available.
    Code:
    include("xmlapi.php");
    
    $ip = "10.1.1.1";
    $root_user = "root";
    $root_pass = "r00ts3cr3t!";
    
    $xmlapi = new xmlapi($ip);
    
    $xmlapi->password_auth($root_user, $root_pass);
    
    $accounts = $xmlapi->listaccts();
    
    // do something with the $accounts object....
    
    Regards,
    -DavidN
     
  5. vanessa

    vanessa Well-Known Member
    PartnerNOC

    Joined:
    Sep 26, 2006
    Messages:
    817
    Likes Received:
    22
    Trophy Points:
    18
    Location:
    Virginia Beach, VA
    cPanel Access Level:
    DataCenter Provider
    This is actually what a lot of hosting providers do - so *they* pick the usernames, instead of cPanel. When it gets to the point where you're managing more than one server, you can't guarantee that one server isn't going to pick a username that already exists on another.
     
  6. meglio

    meglio Registered

    Joined:
    May 3, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    PHP code to select unique username

    Hi. You may be interested - this PHP code I have developed for my own needs and it's well tested over 4000 calls. 2 methods do everything:

    Private Paste - Pastie
     
Loading...

Share This Page