| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  | #!/usr/bin/env php
 | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $root = dirname(dirname(dirname(__FILE__))); | 
					
						
							|  |  |  | require_once $root.'/scripts/__init_script__.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ($argc !== 5) { | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  |   echo pht( | 
					
						
							|  |  |  |     "Usage: %s\n", | 
					
						
							|  |  |  |     'add_user.php <username> <email> <realname> <admin_user>'); | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  |   exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $username = $argv[1]; | 
					
						
							|  |  |  | $email = $argv[2]; | 
					
						
							|  |  |  | $realname = $argv[3]; | 
					
						
							|  |  |  | $admin = $argv[4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $admin = id(new PhabricatorUser())->loadOneWhere( | 
					
						
							|  |  |  |   'username = %s', | 
					
						
							|  |  |  |   $argv[4]); | 
					
						
							|  |  |  | if (!$admin) { | 
					
						
							|  |  |  |   throw new Exception( | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  |     pht( | 
					
						
							|  |  |  |       'Admin user must be the username of a valid Phabricator account, used '. | 
					
						
							|  |  |  |       'to send the new user a welcome email.')); | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $existing_user = id(new PhabricatorUser())->loadOneWhere( | 
					
						
							|  |  |  |   'username = %s', | 
					
						
							|  |  |  |   $username); | 
					
						
							|  |  |  | if ($existing_user) { | 
					
						
							|  |  |  |   throw new Exception( | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  |     pht( | 
					
						
							|  |  |  |       "There is already a user with the username '%s'!", | 
					
						
							|  |  |  |       $username)); | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-07 10:29:33 -07:00
										 |  |  | $existing_email = id(new PhabricatorUserEmail())->loadOneWhere( | 
					
						
							|  |  |  |   'address = %s', | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  |   $email); | 
					
						
							| 
									
										
										
										
											2012-05-07 10:29:33 -07:00
										 |  |  | if ($existing_email) { | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  |   throw new Exception( | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  |     pht( | 
					
						
							|  |  |  |       "There is already a user with the email '%s'!", | 
					
						
							|  |  |  |       $email)); | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $user = new PhabricatorUser(); | 
					
						
							|  |  |  | $user->setUsername($username); | 
					
						
							|  |  |  | $user->setRealname($realname); | 
					
						
							| 
									
										
										
										
											2013-11-13 11:24:56 -08:00
										 |  |  | $user->setIsApproved(1); | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-07 10:29:33 -07:00
										 |  |  | $email_object = id(new PhabricatorUserEmail()) | 
					
						
							|  |  |  |   ->setAddress($email) | 
					
						
							| 
									
										
										
										
											2012-05-25 07:30:44 -07:00
										 |  |  |   ->setIsVerified(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | id(new PhabricatorUserEditor()) | 
					
						
							|  |  |  |   ->setActor($admin) | 
					
						
							|  |  |  |   ->createNewUser($user, $email_object); | 
					
						
							| 
									
										
										
										
											2012-05-07 10:29:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 09:39:27 -08:00
										 |  |  | $welcome_engine = id(new PhabricatorPeopleWelcomeMailEngine()) | 
					
						
							|  |  |  |   ->setSender($admin) | 
					
						
							|  |  |  |   ->setRecipient($user); | 
					
						
							|  |  |  | if ($welcome_engine->canSendMail()) { | 
					
						
							|  |  |  |   $welcome_engine->sendMail(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-01-06 10:44:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  | echo pht( | 
					
						
							|  |  |  |   "Created user '%s' (realname='%s', email='%s').\n", | 
					
						
							|  |  |  |   $username, | 
					
						
							|  |  |  |   $realname, | 
					
						
							|  |  |  |   $email); |