Skip to content

Commit 8d1d561

Browse files
committed
second commit (clean code)
1 parent fdb802c commit 8d1d561

22 files changed

+938
-0
lines changed

.gitignore

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#-------------------------
2+
# Operating Specific Junk Files
3+
#-------------------------
4+
5+
# OS X
6+
.DS_Store
7+
.AppleDouble
8+
.LSOverride
9+
10+
# OS X Thumbnails
11+
._*
12+
13+
# Windows image file caches
14+
Thumbs.db
15+
ehthumbs.db
16+
Desktop.ini
17+
18+
# Recycle Bin used on file shares
19+
$RECYCLE.BIN/
20+
21+
# Windows Installer files
22+
*.cab
23+
*.msi
24+
*.msm
25+
*.msp
26+
27+
# Windows shortcuts
28+
*.lnk
29+
30+
# Linux
31+
*~
32+
33+
# KDE directory preferences
34+
.directory
35+
36+
# Linux trash folder which might appear on any partition or disk
37+
.Trash-*
38+
39+
#-------------------------
40+
# Environment Files
41+
#-------------------------
42+
# These should never be under version control,
43+
# as it poses a security risk.
44+
.env
45+
.vagrant
46+
Vagrantfile
47+
48+
#-------------------------
49+
# Temporary Files
50+
#-------------------------
51+
writable/cache/*
52+
!writable/cache/index.html
53+
54+
writable/logs/*
55+
!writable/logs/index.html
56+
57+
writable/session/*
58+
!writable/session/index.html
59+
60+
writable/uploads/*
61+
!writable/uploads/index.html
62+
63+
writable/debugbar/*
64+
!writable/debugbar/index.html
65+
66+
writable/**/*.db
67+
writable/**/*.sqlite
68+
69+
php_errors.log
70+
71+
#-------------------------
72+
# User Guide Temp Files
73+
#-------------------------
74+
user_guide_src/build/*
75+
76+
#-------------------------
77+
# Test Files
78+
#-------------------------
79+
tests/coverage*
80+
81+
# Don't save phpunit under version control.
82+
phpunit
83+
84+
#-------------------------
85+
# Composer
86+
#-------------------------
87+
vendor/
88+
composer.lock
89+
90+
#-------------------------
91+
# IDE / Development Files
92+
#-------------------------
93+
94+
# Modules Testing
95+
_modules/*
96+
97+
# phpenv local config
98+
.php-version
99+
100+
# Jetbrains editors (PHPStorm, etc)
101+
.idea/
102+
*.iml
103+
104+
# Netbeans
105+
nbproject/
106+
build/
107+
nbbuild/
108+
dist/
109+
nbdist/
110+
nbactions.xml
111+
nb-configuration.xml
112+
.nb-gradle/
113+
114+
# Sublime Text
115+
*.tmlanguage.cache
116+
*.tmPreferences.cache
117+
*.stTheme.cache
118+
*.sublime-workspace
119+
*.sublime-project
120+
.phpintel
121+
/api/
122+
123+
/results/
124+
/phpunit*.xml
125+
126+
/.php-cs-fixer.php

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
"editor.indentSize": "tabSize",
5+
"editor.insertSpaces": false,
6+
"editor.tabSize": 4,
7+
"editor.detectIndentation": true,
8+
"[xml]": {
9+
"editor.defaultFormatter": "DotJoshJohnson.xml"
10+
},
11+
}

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "franky5831/codeigniter4-user-library",
3+
"description": "Codeigniter4 User Mangement Library",
4+
"type": "package",
5+
"version": "0.0.1",
6+
"autoload": {
7+
"psr-4": {
8+
"Franky5831\\CodeIgniter4UserLibrary\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Francesco Ciannavei",
14+
"email": "francesco@ciannavei.dev",
15+
"homepage": "https://www.ciannavei.dev",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php": "^8.1",
21+
"codeigniter4/framework": "^4.0",
22+
"voku/anti-xss": "^4.1"
23+
},
24+
"extra": {
25+
"codeigniter": {
26+
"namespace": "Franky5831\\CodeIgniter4UserLibrary",
27+
"views": "Views"
28+
}
29+
}
30+
}

readme.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Codeigniter 4 User Management Library
2+
3+
This is a library developed for Codeigniter 4 that will make the management of users in your application easier and more secure.
4+
5+
The library will automatically manage the registration and login process, with security measures in pace.
6+
7+
## Security measures:
8+
- [x] Xss
9+
- [x] SQL Injections
10+
- [x] Session Hijacking
11+
- [x] Captchas
12+
- [ ] Brute force attacks
13+
14+
15+
Please remember that this library is still being developed, some features might not be already available and others might not work at all 😊.
16+
17+
18+
## How to install the library:
19+
1. Add the following code block to the composer.json file:
20+
```json
21+
"repositories": [
22+
{
23+
"type": "vcs",
24+
"url": "git@github.com:franky5831/CI4-PCKG-UserLib.git"
25+
}
26+
],
27+
```
28+
Since this is a private repository, you need to add that line to your composer.json file.
29+
30+
2. Run the following command in your terminal:
31+
```bash
32+
composer require franky5831/ci4-pckg-test
33+
```

src/Config/App.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

src/Config/Filters.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
/**
4+
* Adds the redirect to login filter
5+
*/
6+
config('Filters')->aliases["redirect_to_login"] = \Franky5831\CodeIgniter4UserLibrary\Filters\RedirectToLogin::class;
7+
config('Filters')->aliases["session_hijacking"] = \Franky5831\CodeIgniter4UserLibrary\Filters\CheckUserSession::class;
8+
config('Filters')->globals["before"][] = "redirect_to_login";
9+
config('Filters')->globals["before"][] = "session_hijacking";

src/Config/Routes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use CodeIgniter\Router\RouteCollection;
4+
5+
/**
6+
* @var RouteCollection $routes
7+
*/
8+
$routes->match(['get', 'post'], '/login', '\Franky5831\CodeIgniter4UserLibrary\Controllers\User::login', ["as" => "loginurl"]);
9+
$routes->match(['get', 'post'], '/logout', '\Franky5831\CodeIgniter4UserLibrary\Controllers\User::logout', ["as" => "logouturl"]);
10+
$routes->match(['get', 'post'], '/register', '\Franky5831\CodeIgniter4UserLibrary\Controllers\User::register', ["as" => "registerurl"]);

0 commit comments

Comments
 (0)