diff --git a/src/app/SignupCode.php b/src/app/SignupCode.php index f66caef3..1538f03a 100644 --- a/src/app/SignupCode.php +++ b/src/app/SignupCode.php @@ -1,102 +1,92 @@ 'array']; /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = ['expires_at']; /** * Check if code is expired. * * @return bool True if code is expired, False otherwise */ public function isExpired() { return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false; } /** * Generate a short code (for human). * * @return string */ public static function generateShortCode(): string { $code_length = env('SIGNUP_CODE_LENGTH', self::SHORTCODE_LENGTH); - $code_chars = env('SIGNUP_CODE_CHARS', self::SHORTCODE_CHARS); - $random = []; - for ($i = 1; $i <= $code_length; $i++) { - $random[] = $code_chars[rand(0, strlen($code_chars) - 1)]; - } - - shuffle($random); - - return implode('', $random); + return \App\Utils::randStr($code_length); } } diff --git a/src/app/Utils.php b/src/app/Utils.php index 3eb059f2..dbd23d60 100644 --- a/src/app/Utils.php +++ b/src/app/Utils.php @@ -1,164 +1,204 @@ toString(); } private static function combine($input, $r, $index, $data, $i, &$output): void { $n = count($input); // Current cobination is ready if ($index == $r) { $output[] = array_slice($data, 0, $r); return; } // When no more elements are there to put in data[] if ($i >= $n) { return; } // current is included, put next at next location $data[$index] = $input[$i]; self::combine($input, $r, $index + 1, $data, $i + 1, $output); // current is excluded, replace it with next (Note that i+1 // is passed, but index is not changed) self::combine($input, $r, $index, $data, $i + 1, $output); } /** * Create a configuration/environment data to be passed to * the UI * * @todo For a lack of better place this is put here for now * * @return array Configuration data */ public static function uiEnv(): array { $opts = ['app.name', 'app.url', 'app.domain']; $env = \app('config')->getMany($opts); $countries = include resource_path('countries.php'); $env['countries'] = $countries ?: []; $isAdmin = strpos(request()->getHttpHost(), 'admin.') === 0; $env['jsapp'] = $isAdmin ? 'admin.js' : 'user.js'; return $env; } /** * Email address (login or alias) validation * * @param string $email Email address * @param \App\User $user The account owner * @param bool $is_alias The email is an alias * * @return string Error message on validation error */ public static function validateEmail( string $email, \App\User $user, bool $is_alias = false ): ?string { $attribute = $is_alias ? 'alias' : 'email'; if (strpos($email, '@') === false) { return \trans('validation.entryinvalid', ['attribute' => $attribute]); } list($login, $domain) = explode('@', $email); // Check if domain exists $domain = Domain::where('namespace', Str::lower($domain))->first(); if (empty($domain)) { return \trans('validation.domaininvalid'); } // Validate login part alone $v = Validator::make( [$attribute => $login], [$attribute => ['required', new UserEmailLocal(!$domain->isPublic())]] ); if ($v->fails()) { return $v->errors()->toArray()[$attribute][0]; } // Check if it is one of domains available to the user // TODO: We should have a helper that returns "flat" array with domain names // I guess we could use pluck() somehow $domains = array_map( function ($domain) { return $domain->namespace; }, $user->domains() ); if (!in_array($domain->namespace, $domains)) { return \trans('validation.entryexists', ['attribute' => 'domain']); } // Check if user with specified address already exists if (User::findByEmail($email)) { return \trans('validation.entryexists', ['attribute' => $attribute]); } return null; } } diff --git a/src/database/seeds/OpenViduRoomSeeder.php b/src/database/seeds/OpenViduRoomSeeder.php index 5cb80ca7..f4eef6d2 100644 --- a/src/database/seeds/OpenViduRoomSeeder.php +++ b/src/database/seeds/OpenViduRoomSeeder.php @@ -1,24 +1,33 @@ first(); - $room = \App\OpenVidu\Room::create( + $john = \App\User::where('email', 'john@kolab.org')->first(); + $jack = \App\User::where('email', 'jack@kolab.org')->first(); + + \App\OpenVidu\Room::create( [ - 'user_id' => $user->id, + 'user_id' => $john->id, 'name' => 'john' ] ); + + \App\OpenVidu\Room::create( + [ + 'user_id' => $jack->id, + 'name' => strtolower(\App\Utils::randStr(3, 3, '-')) + ] + ); } }