2525 */
2626namespace OC \Core \Command \User ;
2727
28+ use Egulias \EmailValidator \EmailValidator ;
29+ use Egulias \EmailValidator \Validation \RFCValidation ;
2830use OC \Files \Filesystem ;
31+ use OCA \Settings \Mailer \NewUserMailHelper ;
32+ use OCP \IConfig ;
2933use OCP \IGroup ;
3034use OCP \IGroupManager ;
3135use OCP \IUser ;
3236use OCP \IUserManager ;
37+ use OCP \Security \ISecureRandom ;
3338use Symfony \Component \Console \Command \Command ;
3439use Symfony \Component \Console \Helper \QuestionHelper ;
3540use Symfony \Component \Console \Input \InputArgument ;
3944use Symfony \Component \Console \Question \Question ;
4045
4146class Add extends Command {
42- /** @var \OCP\IUserManager */
47+ /**
48+ * @var IUserManager
49+ */
4350 protected $ userManager ;
4451
45- /** @var \OCP\IGroupManager */
52+ /**
53+ * @var IGroupManager
54+ */
4655 protected $ groupManager ;
4756
57+ /**
58+ * @var EmailValidator
59+ */
60+ protected $ emailValidator ;
61+
62+ /**
63+ * @var IConfig
64+ */
65+ private $ config ;
66+
67+ /**
68+ * @var NewUserMailHelper
69+ */
70+ private $ mailHelper ;
71+
72+ /**
73+ * @var ISecureRandom
74+ */
75+ private $ secureRandom ;
76+
4877 /**
4978 * @param IUserManager $userManager
5079 * @param IGroupManager $groupManager
80+ * @param EmailValidator $emailValidator
5181 */
52- public function __construct (IUserManager $ userManager , IGroupManager $ groupManager ) {
82+ public function __construct (
83+ IUserManager $ userManager ,
84+ IGroupManager $ groupManager ,
85+ EmailValidator $ emailValidator ,
86+ IConfig $ config ,
87+ NewUserMailHelper $ mailHelper ,
88+ ISecureRandom $ secureRandom
89+ ) {
5390 parent ::__construct ();
5491 $ this ->userManager = $ userManager ;
5592 $ this ->groupManager = $ groupManager ;
93+ $ this ->emailValidator = $ emailValidator ;
94+ $ this ->config = $ config ;
95+ $ this ->mailHelper = $ mailHelper ;
96+ $ this ->secureRandom = $ secureRandom ;
5697 }
5798
5899 protected function configure () {
@@ -81,11 +122,19 @@ protected function configure() {
81122 'g ' ,
82123 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY ,
83124 'groups the user should be added to (The group will be created if it does not exist) '
125+ )
126+ ->addOption (
127+ 'email ' ,
128+ null ,
129+ InputOption::VALUE_REQUIRED ,
130+ 'When set, users may register using the default E-Mail verification workflow '
84131 );
85132 }
86133
87134 protected function execute (InputInterface $ input , OutputInterface $ output ): int {
88135 $ uid = $ input ->getArgument ('uid ' );
136+ $ emailIsSet = \is_string ($ input ->getOption ('email ' )) && \mb_strlen ($ input ->getOption ('email ' )) > 0 ;
137+
89138 if ($ this ->userManager ->userExists ($ uid )) {
90139 $ output ->writeln ('<error>The user " ' . $ uid . '" already exists.</error> ' );
91140 return 1 ;
@@ -97,6 +146,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
97146 $ output ->writeln ('<error>--password-from-env given, but OC_PASS is empty!</error> ' );
98147 return 1 ;
99148 }
149+ } elseif ($ emailIsSet ) {
150+ // Set a temporary password, an invitation E-Mail will be sent
151+ $ password = \implode ('' , [
152+ $ this ->secureRandom ->generate (10 ),
153+ $ this ->secureRandom ->generate (1 , ISecureRandom::CHAR_UPPER ),
154+ $ this ->secureRandom ->generate (1 , ISecureRandom::CHAR_LOWER ),
155+ $ this ->secureRandom ->generate (1 , ISecureRandom::CHAR_DIGITS ),
156+ $ this ->secureRandom ->generate (1 , ISecureRandom::CHAR_SYMBOLS )
157+ ]);
100158 } elseif ($ input ->isInteractive ()) {
101159 /** @var QuestionHelper $helper */
102160 $ helper = $ this ->getHelper ('question ' );
@@ -118,6 +176,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
118176 return 1 ;
119177 }
120178
179+ $ emailIsValid = false ;
180+
181+ if ($ emailIsSet ) {
182+ $ emailIsValid = $ this ->emailValidator ->isValid ($ input ->getOption ('email ' ), new RFCValidation ());
183+ }
184+
185+ if ($ emailIsSet && !$ emailIsValid ) {
186+ $ output ->writeln (\sprintf (
187+ '<error>The given E-Mail address "%s" is invalid: %s</error> ' ,
188+ $ input ->getOption ('email ' ),
189+ $ this ->emailValidator ->getError ()->description ()
190+ ));
191+
192+ return 1 ;
193+ }
194+
121195 try {
122196 $ user = $ this ->userManager ->createUser (
123197 $ input ->getArgument ('uid ' ),
@@ -128,7 +202,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
128202 return 1 ;
129203 }
130204
131-
132205 if ($ user instanceof IUser) {
133206 $ output ->writeln ('<info>The user " ' . $ user ->getUID () . '" was created successfully</info> ' );
134207 } else {
@@ -141,6 +214,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
141214 $ output ->writeln ('Display name set to " ' . $ user ->getDisplayName () . '" ' );
142215 }
143216
217+ if ($ emailIsSet && $ emailIsValid ) {
218+ $ user ->setSystemEMailAddress ($ input ->getOption ('email ' ));
219+ $ output ->writeln ('E-Mail set to " ' . $ user ->getSystemEMailAddress () . '" ' );
220+
221+ if ($ this ->config ->getAppValue ('core ' , 'newUser.sendEmail ' , 'yes ' ) === 'yes ' ) {
222+ try {
223+ $ this ->mailHelper ->sendMail (
224+ $ user ,
225+ $ this ->mailHelper ->generateTemplate ($ user , true )
226+ );
227+ } catch (\Exception $ e ) {
228+ $ output ->writeln (\sprintf ('Unable to send the invitation mail to %s ' , $ user ->getEMailAddress ()));
229+ }
230+ }
231+ }
232+
144233 $ groups = $ input ->getOption ('group ' );
145234
146235 if (!empty ($ groups )) {
0 commit comments