diff --git a/src/app/SignupCode.php b/src/app/SignupCode.php index 1a11f690..9f44dae0 100644 --- a/src/app/SignupCode.php +++ b/src/app/SignupCode.php @@ -1,106 +1,96 @@ '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() { // @phpstan-ignore-next-line 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 e0ef12dc..4b4724bf 100644 --- a/src/app/Utils.php +++ b/src/app/Utils.php @@ -1,155 +1,195 @@ diffInDays($end) + 1; } /** * Provide all unique combinations of elements in $input, with order and duplicates irrelevant. * * @param array $input The input array of elements. * * @return array[] */ public static function powerSet(array $input): array { $output = []; for ($x = 0; $x < count($input); $x++) { self::combine($input, $x + 1, 0, [], 0, $output); } return $output; } /** * Returns the current user's email address or null. * * @return string */ public static function userEmailOrNull(): ?string { $user = Auth::user(); if (!$user) { return null; } return $user->email; } + /** + * Returns a random string consisting of a quantity of segments of a certain length joined. + * + * Example: + * + * ```php + * $roomName = strtolower(\App\Utils::randStr(3, 3, '-'); + * // $roomName == '3qb-7cs-cjj' + * ``` + * + * @param int $length The length of each segment + * @param int $qty The quantity of segments + * @param string $join The string to use to join the segments + * + * @return string + */ + public static function randStr($length, $qty = 1, $join = '') + { + $chars = env('SHORTCODE_CHARS', self::CHARS); + + $randStrs = []; + + for ($x = 0; $x < $qty; $x++) { + $randStrs[$x] = []; + + for ($y = 0; $y < $length; $y++) { + $randStrs[$x][] = $chars[rand(0, strlen($chars) - 1)]; + } + + shuffle($randStrs[$x]); + + $randStrs[$x] = implode('', $randStrs[$x]); + } + + return implode($join, $randStrs); + } + /** * Returns a UUID in the form of an integer. * * @return integer */ public static function uuidInt(): int { $hex = Uuid::uuid4(); $bin = pack('h*', str_replace('-', '', $hex)); $ids = unpack('L', $bin); $id = array_shift($ids); return $id; } /** * Returns a UUID in the form of a string. * * @return string */ public static function uuidStr(): string { return Uuid::uuid4()->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 self URL * * @param string $route Route/Path * * @return string Full URL */ public static function serviceUrl(string $route): string { $url = \url($route); $app_url = trim(\config('app.url'), '/'); $pub_url = trim(\config('app.public_url'), '/'); if ($pub_url != $app_url) { $url = str_replace($app_url, $pub_url, $url); } return $url; } /** * 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'; $env['paymentProvider'] = \config('services.payment_provider'); $env['stripePK'] = \config('services.stripe.public_key'); return $env; } } diff --git a/src/database/seeds/local/OpenViduRoomSeeder.php b/src/database/seeds/local/OpenViduRoomSeeder.php index 2a4a8026..12b68987 100644 --- a/src/database/seeds/local/OpenViduRoomSeeder.php +++ b/src/database/seeds/local/OpenViduRoomSeeder.php @@ -1,26 +1,35 @@ 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, '-')) + ] + ); } }