77 * @author Joas Schilling <[email protected] > 88 * @author Laurens Post <[email protected] > 99 * @author Roeland Jago Douma <[email protected] > 10+ * @author Anupam Kumar <[email protected] > 1011 *
1112 * @license AGPL-3.0
1213 *
4647use Symfony \Component \Console \Question \Question ;
4748
4849class 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- */
8950 public function __construct (
90- IUserManager $ userManager ,
91- IGroupManager $ groupManager ,
92- EmailValidator $ emailValidator ,
93- IConfig $ config ,
94- NewUserMailHelper $ mailHelper ,
95- IEventDispatcher $ eventDispatcher ,
96- ISecureRandom $ secureRandom
51+ protected IUserManager $ userManager ,
52+ protected IGroupManager $ groupManager ,
53+ protected EmailValidator $ emailValidator ,
54+ private IConfig $ config ,
55+ private NewUserMailHelper $ mailHelper ,
56+ private IEventDispatcher $ eventDispatcher ,
57+ private ISecureRandom $ secureRandom
9758 ) {
9859 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 ;
10660 }
10761
10862 protected function configure () {
@@ -142,23 +96,39 @@ protected function configure() {
14296
14397 protected function execute (InputInterface $ input , OutputInterface $ output ): int {
14498 $ 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-
15099 if ($ this ->userManager ->userExists ($ uid )) {
151100 $ output ->writeln ('<error>The user " ' . $ uid . '" already exists.</error> ' );
152101 return 1 ;
153102 }
154103
104+ $ password = '' ;
105+ $ sendPasswordEmail = false ;
106+
155107 if ($ input ->getOption ('password-from-env ' )) {
156108 $ password = getenv ('OC_PASS ' );
157109
158110 if (!$ password ) {
159111 $ output ->writeln ('<error>--password-from-env given, but OC_PASS is empty!</error> ' );
160112 return 1 ;
161113 }
114+ } elseif (\mb_strlen ($ input ->getOption ('email ' )) > 0 ) {
115+ if (!$ this ->emailValidator ->isValid ($ input ->getOption ('email ' ) ?? '' , new RFCValidation ())) {
116+ $ output ->writeln (\sprintf (
117+ '<error>The given E-Mail address "%s" is invalid: %s</error> ' ,
118+ $ input ->getOption ('email ' ),
119+ $ this ->emailValidator ->getError ()->description ()
120+ ));
121+
122+ return 1 ;
123+ }
124+
125+ $ output ->writeln ('Setting a temporary password. ' );
126+
127+ $ passwordEvent = new GenerateSecurePasswordEvent ();
128+ $ this ->eventDispatcher ->dispatchTyped ($ passwordEvent );
129+ $ password = $ passwordEvent ->getPassword () ?? $ this ->secureRandom ->generate (20 );
130+
131+ $ sendPasswordEmail = true ;
162132 } elseif ($ input ->isInteractive ()) {
163133 /** @var QuestionHelper $helper */
164134 $ helper = $ this ->getHelper ('question ' );
@@ -180,26 +150,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
180150 return 1 ;
181151 }
182152
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-
199153 try {
200154 $ user = $ this ->userManager ->createUser (
201155 $ input ->getArgument ('uid ' ),
202- $ password ?: $ temporaryPassword
156+ $ password,
203157 );
204158 } catch (\Exception $ e ) {
205159 $ output ->writeln ('<error> ' . $ e ->getMessage () . '</error> ' );
@@ -218,23 +172,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
218172 $ output ->writeln (sprintf ('Display name set to "%s" ' , $ user ->getDisplayName ()));
219173 }
220174
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- }
236- }
237-
238175 $ groups = $ input ->getOption ('group ' );
239176
240177 if (!empty ($ groups )) {
@@ -257,18 +194,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
257194 $ output ->writeln ('User " ' . $ user ->getUID () . '" added to group " ' . $ group ->getGID () . '" ' );
258195 }
259196 }
260- return 0 ;
261- }
262197
263- /**
264- * @return string
265- */
266- protected function getTemporaryPassword (): string
267- {
268- $ passwordEvent = new GenerateSecurePasswordEvent ();
198+ // Send email to user if we set a temporary password
199+ if ($ sendPasswordEmail ) {
200+ $ email = $ input ->getOption ('email ' );
201+ $ user ->setSystemEMailAddress ($ email );
269202
270- $ this ->eventDispatcher ->dispatchTyped ($ passwordEvent );
203+ if ($ this ->config ->getAppValue ('core ' , 'newUser.sendEmail ' , 'yes ' ) === 'yes ' ) {
204+ try {
205+ $ this ->mailHelper ->sendMail ($ user , $ this ->mailHelper ->generateTemplate ($ user , true ));
206+ $ output ->writeln (\sprintf ('Invitation E-Mail sent to %s. ' , $ email ));
207+ } catch (\Exception $ e ) {
208+ $ output ->writeln (\sprintf ('Unable to send the invitation mail to %s ' , $ email ));
209+ }
210+ }
211+ }
271212
272- return $ passwordEvent -> getPassword () ?? $ this -> secureRandom -> generate ( 20 ) ;
213+ return 0 ;
273214 }
274215}
0 commit comments