|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Franky5831\CodeIgniter4UserLibrary\Config; |
| 4 | + |
| 5 | +use \Config\App as AppConfig; |
| 6 | + |
| 7 | +class App extends \Config\App |
| 8 | +{ |
| 9 | + /* |
| 10 | + |-------------------------------------------------------------------------- |
| 11 | + | UserLib |
| 12 | + |-------------------------------------------------------------------------- |
| 13 | + | |
| 14 | + | UserLib is a CodeIgniter 4 package that provides a way to add simple user management to your application. |
| 15 | + | |
| 16 | + */ |
| 17 | + |
| 18 | + /* |
| 19 | + * Enable Registration |
| 20 | + */ |
| 21 | + public bool $userCanRegister = true; |
| 22 | + |
| 23 | + /* |
| 24 | + * Decide if the user can view all pages by default. |
| 25 | + * This rule can be overrided by using the method userCanView() or userCanNotView() from the user helper. |
| 26 | + * TODO: Implement this |
| 27 | + */ |
| 28 | + public bool $userCanViewByDefault = false; |
| 29 | + |
| 30 | + /* |
| 31 | + * Enable Login |
| 32 | + */ |
| 33 | + public bool $userLibLogin = true; |
| 34 | + |
| 35 | + /* |
| 36 | + * Enable Captcha |
| 37 | + */ |
| 38 | + public bool $userLibCaptcha = true; |
| 39 | + |
| 40 | + /* |
| 41 | + * Captcha Type |
| 42 | + * Available types: cloudflare, recaptcha |
| 43 | + */ |
| 44 | + public string $userLibCaptchaType = 'cloudflare'; |
| 45 | + |
| 46 | + /* |
| 47 | + * Captcha Options |
| 48 | + * Example: options for the cloudflare captcha: siteKey and secretKey |
| 49 | + * This is just an example of the structure, the content of the array gets emptied from the constructor |
| 50 | + */ |
| 51 | + public array $userLibCaptchaOptions = [ |
| 52 | + "cloudflare" => [ |
| 53 | + "siteKey" => "", |
| 54 | + "secretKey" => "", |
| 55 | + ], |
| 56 | + "recaptcha-v3" => [ |
| 57 | + "siteKey" => "", |
| 58 | + "secretKey" => "", |
| 59 | + ] |
| 60 | + ]; |
| 61 | + |
| 62 | + /* |
| 63 | + * User extra attributes |
| 64 | + * Example: name, phone, address, etc. |
| 65 | + * This is just an example of the structure, the content of the array gets emptied from the constructor |
| 66 | + ! WARNING: You also need to create the column on the database |
| 67 | + */ |
| 68 | + public array $userExtraAttributes = [ |
| 69 | + "name" => [ |
| 70 | + "label" => "Name", |
| 71 | + "type" => "text", |
| 72 | + "rules" => "required|max_length[255]", |
| 73 | + ], |
| 74 | + "username" => [ |
| 75 | + "label" => "Username", |
| 76 | + "type" => "text", |
| 77 | + "rules" => "required|max_length[255]", |
| 78 | + ], |
| 79 | + "phone" => [ |
| 80 | + "label" => "Phone", |
| 81 | + "type" => "text", |
| 82 | + "rules" => "required|max_length[255]|regex_match[/^[0-9]{10}$/]", |
| 83 | + ] |
| 84 | + ]; |
| 85 | + |
| 86 | + |
| 87 | + public function __construct() |
| 88 | + { |
| 89 | + $appConfig = config(AppConfig::class); |
| 90 | + |
| 91 | + $this->userLibCaptchaOptions = array(); |
| 92 | + $this->userExtraAttributes = array(); |
| 93 | + $this->userCanRegister = property_exists($appConfig, "userCanRegister") ? $appConfig->userCanRegister : $this->userCanRegister; |
| 94 | + $this->userCanViewByDefault = property_exists($appConfig, "userCanViewByDefault") ? $appConfig->userCanViewByDefault : $this->userCanViewByDefault; |
| 95 | + $this->userLibLogin = property_exists($appConfig, "userLibLogin") ? $appConfig->userLibLogin : $this->userLibLogin; |
| 96 | + $this->userLibCaptcha = property_exists($appConfig, "userLibCaptcha") ? $appConfig->userLibCaptcha : $this->userLibCaptcha; |
| 97 | + $this->userLibCaptchaType = property_exists($appConfig, "userLibCaptchaType") ? $appConfig->userLibCaptchaType : $this->userLibCaptchaType; |
| 98 | + $this->userLibCaptchaOptions = property_exists($appConfig, "userLibCaptchaOptions") ? $appConfig->userLibCaptchaOptions : $this->userLibCaptchaOptions; |
| 99 | + $this->userExtraAttributes = property_exists($appConfig, "userExtraAttributes") ? $appConfig->userExtraAttributes : $this->userExtraAttributes; |
| 100 | + |
| 101 | + $allowedCaptchas = ["cloudflare", "recaptcha-v3"]; |
| 102 | + if ( |
| 103 | + !in_array($this->userLibCaptchaType, $allowedCaptchas) |
| 104 | + && $this->userLibCaptcha |
| 105 | + ) { |
| 106 | + throw new \Exception("The selected captcha type does not exists", 1); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments