4343
4444@ APICommand (name = "ldapCreateAccount" , description = "Creates an account from an LDAP user" , responseObject = AccountResponse .class , since = "4.2.0" )
4545public class LdapCreateAccountCmd extends BaseCmd {
46- public static final Logger s_logger = Logger
47- .getLogger (LdapCreateAccountCmd .class .getName ());
48- private static final String s_name = "createaccountresponse" ;
49-
50- @ Inject
51- private LdapManager _ldapManager ;
52-
53- @ Parameter (name = ApiConstants .ACCOUNT , type = CommandType .STRING , description = "Creates the user under the specified account. If no account is specified, the username will be used as the account name." )
54- private String accountName ;
55-
56- @ Parameter (name = ApiConstants .ACCOUNT_TYPE , type = CommandType .SHORT , required = true , description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin" )
57- private Short accountType ;
58-
59- @ Parameter (name = ApiConstants .DOMAIN_ID , type = CommandType .UUID , entityType = DomainResponse .class , description = "Creates the user under the specified domain." )
60- private Long domainId ;
61-
62- @ Parameter (name = ApiConstants .TIMEZONE , type = CommandType .STRING , description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format." )
63- private String timezone ;
64-
65- @ Parameter (name = ApiConstants .USERNAME , type = CommandType .STRING , required = true , description = "Unique username." )
66- private String username ;
67-
68- @ Parameter (name = ApiConstants .NETWORK_DOMAIN , type = CommandType .STRING , description = "Network domain for the account's networks" )
69- private String networkDomain ;
70-
71- @ Parameter (name = ApiConstants .ACCOUNT_DETAILS , type = CommandType .MAP , description = "details for account used to store specific parameters" )
72- private Map <String , String > details ;
73-
74- @ Parameter (name = ApiConstants .ACCOUNT_ID , type = CommandType .STRING , description = "Account UUID, required for adding account from external provisioning system" )
75- private String accountUUID ;
76-
77- @ Parameter (name = ApiConstants .USER_ID , type = CommandType .STRING , description = "User UUID, required for adding account from external provisioning system" )
78- private String userUUID ;
79-
80- public LdapCreateAccountCmd () {
81- super ();
82- }
83-
84- public LdapCreateAccountCmd (final LdapManager ldapManager ,
85- final AccountService accountService ) {
86- super ();
87- _ldapManager = ldapManager ;
88- _accountService = accountService ;
89- }
90-
91- UserAccount createCloudstackUserAccount (final LdapUser user ) {
92- return _accountService .createUserAccount (username , generatePassword (),
93- user .getFirstname (), user .getLastname (), user .getEmail (),
94- timezone , accountName , accountType , domainId , networkDomain ,
95- details , accountUUID , userUUID );
96- }
97-
98- @ Override
99- public void execute () throws ServerApiException {
100- final CallContext callContext = getCurrentContext ();
101- callContext .setEventDetails ("Account Name: " + accountName
102- + ", Domain Id:" + domainId );
103- try {
104- final LdapUser user = _ldapManager .getUser (username );
105- validateUser (user );
106- final UserAccount userAccount = createCloudstackUserAccount (user );
107- if (userAccount != null ) {
108- final AccountResponse response = _responseGenerator
109- .createUserAccountResponse (userAccount );
110- response .setResponseName (getCommandName ());
111- setResponseObject (response );
112- } else {
113- throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR ,
114- "Failed to create a user account" );
115- }
116- } catch (final NamingException e ) {
117- throw new ServerApiException (
118- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR ,
119- "No LDAP user exists with the username of " + username );
120- }
121- }
122-
123- private String generatePassword () throws ServerApiException {
124- try {
125- final SecureRandom randomGen = SecureRandom .getInstance ("SHA1PRNG" );
126- final byte bytes [] = new byte [20 ];
127- randomGen .nextBytes (bytes );
128- return Base64 .encode (bytes ).toString ();
129- } catch (final NoSuchAlgorithmException e ) {
130- throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR ,
131- "Failed to generate random password" );
132- }
133- }
134-
135- @ Override
136- public String getCommandName () {
137- return s_name ;
138- }
139-
140- CallContext getCurrentContext () {
141- return CallContext .current ();
142- }
143-
144- @ Override
145- public long getEntityOwnerId () {
146- return Account .ACCOUNT_ID_SYSTEM ;
147- }
148-
149- private boolean validateUser (final LdapUser user ) throws ServerApiException {
150- if (user .getEmail () == null ) {
151- throw new ServerApiException (
152- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
153- + " has no email address set within LDAP" );
154- }
155- if (user .getFirstname () == null ) {
156- throw new ServerApiException (
157- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
158- + " has no firstname set within LDAP" );
159- }
160- if (user .getLastname () == null ) {
161- throw new ServerApiException (
162- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
163- + " has no lastname set within LDAP" );
164- }
165- return true ;
166- }
46+ public static final Logger s_logger = Logger
47+ .getLogger (LdapCreateAccountCmd .class .getName ());
48+ private static final String s_name = "createaccountresponse" ;
49+
50+ @ Inject
51+ private LdapManager _ldapManager ;
52+
53+ @ Parameter (name = ApiConstants .ACCOUNT , type = CommandType .STRING , description = "Creates the user under the specified account. If no account is specified, the username will be used as the account name." )
54+ private String accountName ;
55+
56+ @ Parameter (name = ApiConstants .ACCOUNT_TYPE , type = CommandType .SHORT , required = true , description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin" )
57+ private Short accountType ;
58+
59+ @ Parameter (name = ApiConstants .DOMAIN_ID , type = CommandType .UUID , entityType = DomainResponse .class , description = "Creates the user under the specified domain." )
60+ private Long domainId ;
61+
62+ @ Parameter (name = ApiConstants .TIMEZONE , type = CommandType .STRING , description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format." )
63+ private String timezone ;
64+
65+ @ Parameter (name = ApiConstants .USERNAME , type = CommandType .STRING , required = true , description = "Unique username." )
66+ private String username ;
67+
68+ @ Parameter (name = ApiConstants .NETWORK_DOMAIN , type = CommandType .STRING , description = "Network domain for the account's networks" )
69+ private String networkDomain ;
70+
71+ @ Parameter (name = ApiConstants .ACCOUNT_DETAILS , type = CommandType .MAP , description = "details for account used to store specific parameters" )
72+ private Map <String , String > details ;
73+
74+ @ Parameter (name = ApiConstants .ACCOUNT_ID , type = CommandType .STRING , description = "Account UUID, required for adding account from external provisioning system" )
75+ private String accountUUID ;
76+
77+ @ Parameter (name = ApiConstants .USER_ID , type = CommandType .STRING , description = "User UUID, required for adding account from external provisioning system" )
78+ private String userUUID ;
79+
80+ public LdapCreateAccountCmd () {
81+ super ();
82+ }
83+
84+ public LdapCreateAccountCmd (final LdapManager ldapManager ,
85+ final AccountService accountService ) {
86+ super ();
87+ _ldapManager = ldapManager ;
88+ _accountService = accountService ;
89+ }
90+
91+ UserAccount createCloudstackUserAccount (final LdapUser user ) {
92+ return _accountService .createUserAccount (username , generatePassword (),
93+ user .getFirstname (), user .getLastname (), user .getEmail (),
94+ timezone , accountName , accountType , domainId , networkDomain ,
95+ details , accountUUID , userUUID );
96+ }
97+
98+ @ Override
99+ public void execute () throws ServerApiException {
100+ final CallContext callContext = getCurrentContext ();
101+ callContext .setEventDetails ("Account Name: " + accountName
102+ + ", Domain Id:" + domainId );
103+ try {
104+ final LdapUser user = _ldapManager .getUser (username );
105+ validateUser (user );
106+ final UserAccount userAccount = createCloudstackUserAccount (user );
107+ if (userAccount != null ) {
108+ final AccountResponse response = _responseGenerator
109+ .createUserAccountResponse (userAccount );
110+ response .setResponseName (getCommandName ());
111+ setResponseObject (response );
112+ } else {
113+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR ,
114+ "Failed to create a user account" );
115+ }
116+ } catch (final NamingException e ) {
117+ throw new ServerApiException (
118+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR ,
119+ "No LDAP user exists with the username of " + username );
120+ }
121+ }
122+
123+ private String generatePassword () throws ServerApiException {
124+ try {
125+ final SecureRandom randomGen = SecureRandom .getInstance ("SHA1PRNG" );
126+ final byte bytes [] = new byte [20 ];
127+ randomGen .nextBytes (bytes );
128+ return Base64 .encode (bytes ).toString ();
129+ } catch (final NoSuchAlgorithmException e ) {
130+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR ,
131+ "Failed to generate random password" );
132+ }
133+ }
134+
135+ @ Override
136+ public String getCommandName () {
137+ return s_name ;
138+ }
139+
140+ CallContext getCurrentContext () {
141+ return CallContext .current ();
142+ }
143+
144+ @ Override
145+ public long getEntityOwnerId () {
146+ return Account .ACCOUNT_ID_SYSTEM ;
147+ }
148+
149+ private boolean validateUser (final LdapUser user ) throws ServerApiException {
150+ if (user .getEmail () == null ) {
151+ throw new ServerApiException (
152+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
153+ + " has no email address set within LDAP" );
154+ }
155+ if (user .getFirstname () == null ) {
156+ throw new ServerApiException (
157+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
158+ + " has no firstname set within LDAP" );
159+ }
160+ if (user .getLastname () == null ) {
161+ throw new ServerApiException (
162+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
163+ + " has no lastname set within LDAP" );
164+ }
165+ return true ;
166+ }
167167}
0 commit comments