22/**
33 * @copyright Copyright (c) 2016, ownCloud, Inc.
44 *
5+ * @author Anupam Kumar <[email protected] > 56 * @author Arthur Schiwon <[email protected] > 67 * @author Christoph Wurst <[email protected] > 78 * @author Joas Schilling <[email protected] > 2526 */
2627namespace OC \Core \Command \User ;
2728
28- use Egulias \EmailValidator \EmailValidator ;
29- use Egulias \EmailValidator \Validation \RFCValidation ;
3029use OC \Files \Filesystem ;
3130use OCA \Settings \Mailer \NewUserMailHelper ;
3231use OCP \EventDispatcher \IEventDispatcher ;
3534use OCP \IGroupManager ;
3635use OCP \IUser ;
3736use OCP \IUserManager ;
37+ use OCP \Mail \IMailer ;
3838use OCP \Security \Events \GenerateSecurePasswordEvent ;
3939use OCP \Security \ISecureRandom ;
4040use Symfony \Component \Console \Command \Command ;
4646use Symfony \Component \Console \Question \Question ;
4747
4848class Add extends Command {
49- /**
50- * @var IUserManager
51- */
52- protected $ userManager ;
53-
54- /**
55- * @var IGroupManager
56- */
57- protected $ groupManager ;
58-
59- /**
60- * @var EmailValidator
61- */
62- protected $ emailValidator ;
63-
64- /**
65- * @var IConfig
66- */
67- private $ config ;
68-
69- /**
70- * @var NewUserMailHelper
71- */
72- private $ mailHelper ;
73-
74- /**
75- * @var IEventDispatcher
76- */
77- private $ eventDispatcher ;
78-
79- /**
80- * @var ISecureRandom
81- */
82- private $ secureRandom ;
83-
84- /**
85- * @param IUserManager $userManager
86- * @param IGroupManager $groupManager
87- * @param EmailValidator $emailValidator
88- */
8949 public function __construct (
90- IUserManager $ userManager ,
91- IGroupManager $ groupManager ,
92- EmailValidator $ emailValidator ,
93- IConfig $ config ,
94- NewUserMailHelper $ mailHelper ,
95- IEventDispatcher $ eventDispatcher ,
96- ISecureRandom $ secureRandom
50+ protected IUserManager $ userManager ,
51+ protected IGroupManager $ groupManager ,
52+ protected IMailer $ mailer ,
53+ private IConfig $ config ,
54+ private NewUserMailHelper $ mailHelper ,
55+ private IEventDispatcher $ eventDispatcher ,
56+ private ISecureRandom $ secureRandom
9757 ) {
9858 parent ::__construct ();
99- $ this ->userManager = $ userManager ;
100- $ this ->groupManager = $ groupManager ;
101- $ this ->emailValidator = $ emailValidator ;
102- $ this ->config = $ config ;
103- $ this ->mailHelper = $ mailHelper ;
104- $ this ->eventDispatcher = $ eventDispatcher ;
105- $ this ->secureRandom = $ secureRandom ;
10659 }
10760
10861 protected function configure () {
@@ -142,23 +95,38 @@ protected function configure() {
14295
14396 protected function execute (InputInterface $ input , OutputInterface $ output ): int {
14497 $ uid = $ input ->getArgument ('uid ' );
145- $ emailIsSet = \is_string ($ input ->getOption ('email ' )) && \mb_strlen ($ input ->getOption ('email ' )) > 0 ;
146- $ emailIsValid = $ this ->emailValidator ->isValid ($ input ->getOption ('email ' ) ?? '' , new RFCValidation ());
147- $ password = '' ;
148- $ temporaryPassword = '' ;
149-
15098 if ($ this ->userManager ->userExists ($ uid )) {
15199 $ output ->writeln ('<error>The user " ' . $ uid . '" already exists.</error> ' );
152100 return 1 ;
153101 }
154102
103+ $ password = '' ;
104+ $ sendPasswordEmail = false ;
105+
155106 if ($ input ->getOption ('password-from-env ' )) {
156107 $ password = getenv ('OC_PASS ' );
157108
158109 if (!$ password ) {
159110 $ output ->writeln ('<error>--password-from-env given, but OC_PASS is empty!</error> ' );
160111 return 1 ;
161112 }
113+ } elseif (\mb_strlen ($ input ->getOption ('email ' )) > 0 ) {
114+ if (!$ this ->mailer ->validateMailAddress ($ input ->getOption (('email ' )))) {
115+ $ output ->writeln (\sprintf (
116+ '<error>The given E-Mail address "%s" is invalid</error> ' ,
117+ $ input ->getOption ('email ' ),
118+ ));
119+
120+ return 1 ;
121+ }
122+
123+ $ output ->writeln ('Setting a temporary password. ' );
124+
125+ $ passwordEvent = new GenerateSecurePasswordEvent ();
126+ $ this ->eventDispatcher ->dispatchTyped ($ passwordEvent );
127+ $ password = $ passwordEvent ->getPassword () ?? $ this ->secureRandom ->generate (20 );
128+
129+ $ sendPasswordEmail = true ;
162130 } elseif ($ input ->isInteractive ()) {
163131 /** @var QuestionHelper $helper */
164132 $ helper = $ this ->getHelper ('question ' );
@@ -180,26 +148,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
180148 return 1 ;
181149 }
182150
183- if (trim ($ password ) === '' && $ emailIsSet ) {
184- if ($ emailIsValid ) {
185- $ output ->writeln ('Setting a temporary password. ' );
186-
187- $ temporaryPassword = $ this ->getTemporaryPassword ();
188- } else {
189- $ output ->writeln (\sprintf (
190- '<error>The given E-Mail address "%s" is invalid: %s</error> ' ,
191- $ input ->getOption ('email ' ),
192- $ this ->emailValidator ->getError ()->description ()
193- ));
194-
195- return 1 ;
196- }
197- }
198-
199151 try {
200152 $ user = $ this ->userManager ->createUser (
201153 $ input ->getArgument ('uid ' ),
202- $ password ?: $ temporaryPassword
154+ $ password,
203155 );
204156 } catch (\Exception $ e ) {
205157 $ output ->writeln ('<error> ' . $ e ->getMessage () . '</error> ' );
@@ -215,24 +167,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
215167
216168 if ($ input ->getOption ('display-name ' )) {
217169 $ user ->setDisplayName ($ input ->getOption ('display-name ' ));
218- $ output ->writeln (sprintf ('Display name set to "%s" ' , $ user ->getDisplayName ()));
219- }
220-
221- if ($ emailIsSet && $ emailIsValid ) {
222- $ user ->setSystemEMailAddress ($ input ->getOption ('email ' ));
223- $ output ->writeln (sprintf ('E-Mail set to "%s" ' , (string ) $ user ->getSystemEMailAddress ()));
224-
225- if (trim ($ password ) === '' && $ this ->config ->getAppValue ('core ' , 'newUser.sendEmail ' , 'yes ' ) === 'yes ' ) {
226- try {
227- $ this ->mailHelper ->sendMail (
228- $ user ,
229- $ this ->mailHelper ->generateTemplate ($ user , true )
230- );
231- $ output ->writeln ('Invitation E-Mail sent. ' );
232- } catch (\Exception $ e ) {
233- $ output ->writeln (\sprintf ('Unable to send the invitation mail to %s ' , $ user ->getEMailAddress ()));
234- }
235- }
170+ $ output ->writeln (\sprintf ('Display name set to "%s" ' , $ user ->getDisplayName ()));
236171 }
237172
238173 $ groups = $ input ->getOption ('group ' );
@@ -257,18 +192,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
257192 $ output ->writeln ('User " ' . $ user ->getUID () . '" added to group " ' . $ group ->getGID () . '" ' );
258193 }
259194 }
260- return 0 ;
261- }
262195
263- /**
264- * @return string
265- */
266- protected function getTemporaryPassword (): string
267- {
268- $ passwordEvent = new GenerateSecurePasswordEvent ();
196+ // Send email to user if we set a temporary password
197+ if ($ sendPasswordEmail && $ this ->config ->getAppValue ('core ' , 'newUser.sendEmail ' , 'yes ' ) === 'yes ' ) {
198+ $ email = $ input ->getOption ('email ' );
199+ $ user ->setSystemEMailAddress ($ email );
269200
270- $ this ->eventDispatcher ->dispatchTyped ($ passwordEvent );
201+ try {
202+ $ this ->mailHelper ->sendMail ($ user , $ this ->mailHelper ->generateTemplate ($ user , true ));
203+ $ output ->writeln (\sprintf ('Invitation E-Mail sent to %s ' , $ email ));
204+ } catch (\Exception $ e ) {
205+ $ output ->writeln (\sprintf ('Unable to send the invitation mail to %s ' , $ email ));
206+ }
207+ }
271208
272- return $ passwordEvent -> getPassword () ?? $ this -> secureRandom -> generate ( 20 ) ;
209+ return 0 ;
273210 }
274211}
0 commit comments