Skip to content

Commit 38af4bb

Browse files
committed
Initial commit
0 parents  commit 38af4bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3452
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.*
2+
!/.gitignore

Module.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace XOUser;
3+
4+
class Module
5+
{
6+
public function getConfig()
7+
{
8+
return include __DIR__ . '/config/module.config.php';
9+
}
10+
11+
public function getControllerConfig()
12+
{
13+
return include __DIR__ . '/config/controller.config.php';
14+
}
15+
16+
public function getServiceConfig()
17+
{
18+
return include __DIR__ . '/config/service.config.php';
19+
}
20+
21+
public function getAutoloaderConfig()
22+
{
23+
return [
24+
'Zend\Loader\StandardAutoloader' => [
25+
'namespaces' => [
26+
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
27+
],
28+
],
29+
];
30+
}
31+
}

README.md

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
Introduction
2+
------------
3+
4+
XOUser is a skeleton module for user authentication and registration for Zend Framework 2. This is for making a user module very fast by extending its available functionality.
5+
6+
XOUser stores session data in database. This is mainly a combination of Zend\Db, Zend\Session and Zend\Authentication for managing users persistently.
7+
8+
9+
Features
10+
--------
11+
12+
* User login - authenticate via username or email (by specifying one of these two).
13+
* User registration.
14+
* User change-password.
15+
* Forms protected against CSRF.
16+
17+
Installation
18+
------------
19+
20+
### Database Config:
21+
22+
XOUser expects and assumes you have a valid database configuration under a top key named `db`.
23+
24+
### Database Tables:
25+
26+
XOUser expects two database tables named `users` and `session` for managing users and sessions respectively:
27+
28+
```sql
29+
CREATE TABLE IF NOT EXISTS `users` (
30+
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
31+
`email` varchar(100) DEFAULT NULL,
32+
`username` varchar(100) DEFAULT NULL,
33+
`password` varchar(60) DEFAULT NULL,
34+
`modifiedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
35+
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
36+
PRIMARY KEY(`id`),
37+
KEY `idx_email` (`email`),
38+
KEY `idx_username` (`username`)
39+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
40+
INSERT INTO `users` (`id`, `email`, `username`, `password`, `modifiedAt`, `createdAt`) VALUES (1, '[email protected]', 'admin', '$2y$10$iMDN8kS81DAdHy9/zNd3we2ChPwhy2bTkVIsCyHpNtaNZl9zUuyxG', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
41+
42+
CREATE TABLE IF NOT EXISTS `session` (
43+
`id` char(32) NOT NULL,
44+
`name` char(32) NOT NULL,
45+
`modified` int(11) DEFAULT NULL,
46+
`lifetime` int(11) DEFAULT NULL,
47+
`data` text,
48+
PRIMARY KEY (`id`),
49+
KEY `idx_name` (`name`)
50+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
51+
52+
```
53+
54+
### Session Config:
55+
56+
If you want to set custom configuration for handling session, you should do that under top key `session_config`. If you do not already have a custom configuration for your session, put the following in `./config/module.config.php`:
57+
58+
```php
59+
<?php
60+
61+
'session_config' => array(
62+
'name' => 'session_name',
63+
'use_cookies' => true,
64+
'cookie_lifetime' => 0,
65+
'gc_maxlifetime' => 3600,
66+
),
67+
68+
```
69+
70+
### Usage:
71+
72+
Use the following snippet of code in your controller method to manage login action. For more information, you should check the `LoginController`'s `processAction` method.
73+
74+
```php
75+
<?php
76+
77+
$auth = $this->getAuthService()
78+
->getAdapter()
79+
->setIdentity($data['username'])
80+
->setCredential($data['password'])
81+
->setIdentityType('username'); // This can only be 'username' and 'email'
82+
83+
$result = $this->getAuthService()->authenticate();
84+
85+
if ($result->isValid()) {
86+
// Do something
87+
} else {
88+
// Do something
89+
}
90+
91+
```
92+
93+
Next up, just use this over and over again where you need:
94+
95+
```php
96+
<?php
97+
98+
if (!$this->getAuthService()->hasIdentity()) {
99+
return $this->redirect()->toRoute('auth');
100+
}
101+
```
102+
103+
Available Routes
104+
----------------
105+
106+
```php
107+
108+
/auth
109+
/auth/login
110+
/auth/signup
111+
/auth/change-password
112+
/auth/logout
113+
114+
```
115+
116+
Go to your site: http://yoursite.dev/auth and you should see a login page.
117+
118+
119+
Login
120+
-----
121+
122+
```php
123+
<?php
124+
125+
username: admin
126+
password: 12345678
127+
128+
```
129+
130+
Password Hash Caution
131+
---------------------
132+
133+
**DO NOT CHANGE THE PASSWORD HASH SETTINGS FROM THEIR DEFAULTS** unless you
134+
have fully understood exactly what and why you are doing!
135+
136+
ZF2 Components
137+
--------------
138+
139+
The following ZF2 components are considerably used in XOUser module:
140+
141+
* [Zend/Authentication](https://framework.zend.com/manual/2.4/en/modules/zend.authentication.intro.html)
142+
* [Zend/Crypt](https://framework.zend.com/manual/2.4/en/modules/zend.crypt.introduction.html)
143+
* [Zend/Db](https://framework.zend.com/manual/2.4/en/modules/zend.db.adapter.html)
144+
* [Zend/Filter](https://framework.zend.com/manual/2.4/en/modules/zend.filter.html)
145+
* [Zend/Form](https://framework.zend.com/manual/2.4/en/modules/zend.form.intro.html)
146+
* [Zend/InputFilter](https://framework.zend.com/manual/2.4/en/modules/zend.input-filter.intro.html)
147+
* [Zend/Json](https://framework.zend.com/manual/2.4/en/modules/zend.json.introduction.html)
148+
* [Zend/ModuleManager](https://framework.zend.com/manual/2.4/en/modules/zend.module-manager.intro.html)
149+
* [Zend/Mvc](https://framework.zend.com/manual/2.4/en/modules/zend.mvc.intro.html)
150+
* [Zend/ServiceManager](https://framework.zend.com/manual/2.4/en/modules/zend.service-manager.html)
151+
* [Zend/Session](https://framework.zend.com/manual/2.4/en/modules/zend.session.config.html)
152+
* [Zend/Validator](https://framework.zend.com/manual/2.4/en/modules/zend.validator.html)
153+
* [Zend/View](https://framework.zend.com/manual/2.4/en/modules/zend.view.quick-start.html)
154+
155+
License
156+
-------
157+
158+
This ZF2 module released under MIT license.

composer.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
{
3+
"name": "unclexo/xo-user",
4+
"type": "library",
5+
"description": "A ZF2 module for user authentication which manages user sessions in database.",
6+
"keywords": ["Auth","zf2","Db","Session",],
7+
"homepage": "https://github.com/unclexo/XOUser",
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Jobaer",
12+
"email": "[email protected]",
13+
"homepage": "http://unclexo.com",
14+
"role": "Developer"
15+
}
16+
],
17+
"require": {
18+
"php": ">=5.3.3"
19+
},
20+
}

config/controller.config.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return array(
4+
'factories' => array(
5+
'xodemo' => 'XOUser\Factory\Controller\DemoControllerFactory',
6+
'xologin' => 'XOUser\Factory\Controller\LoginControllerFactory',
7+
'xosignup' => 'XOUser\Factory\Controller\SignupControllerFactory',
8+
'xochangepassword' => 'XOUser\Factory\Controller\ChangePasswordControllerFactory',
9+
),
10+
);

config/module.config.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
return array(
4+
'session_config' => array(
5+
'name' => 'tuesday25',
6+
'use_cookies' => true,
7+
'cookie_lifetime' => 0,
8+
'gc_maxlifetime' => 3600,
9+
),
10+
'view_manager' => array(
11+
'template_path_stack' => array(
12+
__DIR__ . '/../view'
13+
),
14+
),
15+
'router' => array(
16+
'routes' => array(
17+
'auth' => array(
18+
'type' => 'Literal',
19+
'options' => array(
20+
'route' => '/auth',
21+
'defaults' => array(
22+
'controller' => 'xologin',
23+
'action' => 'index',
24+
),
25+
),
26+
'may_terminate' => true,
27+
'child_routes' => array(
28+
'login' => array(
29+
'type' => 'Segment',
30+
'options' => array(
31+
'route' => '/login[/:action]',
32+
'constraints' => array(
33+
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
34+
),
35+
'defaults' => array(
36+
'controller' => 'xologin',
37+
'action' => 'index',
38+
),
39+
),
40+
),
41+
'logout' => array(
42+
'type' => 'Literal',
43+
'options' => array(
44+
'route' => '/logout',
45+
'defaults' => array(
46+
'controller' => 'xologin',
47+
'action' => 'logout',
48+
),
49+
),
50+
),
51+
'signup' => array(
52+
'type' => 'Segment',
53+
'options' => array(
54+
'route' => '/signup[/:action]',
55+
'constraints' => array(
56+
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
57+
),
58+
'defaults' => array(
59+
'controller' => 'xosignup',
60+
'action' => 'index',
61+
),
62+
),
63+
),
64+
'change_password' => array(
65+
'type' => 'Segment',
66+
'options' => array(
67+
'route' => '/change-password[/:action]',
68+
'constraints' => array(
69+
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
70+
),
71+
'defaults' => array(
72+
'controller' => 'xochangepassword',
73+
'action' => 'index',
74+
),
75+
),
76+
),
77+
),
78+
),
79+
80+
// Edit this route as it is used for testing
81+
// purpose only for the admin area
82+
'demo' => array(
83+
'type' => 'Literal',
84+
'options' => array(
85+
'route' => '/demo-admin-area',
86+
'defaults' => array(
87+
'controller' => 'xodemo',
88+
'action' => 'index',
89+
),
90+
),
91+
),
92+
),
93+
),
94+
);

config/service.config.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
return array(
3+
'factories' => array(
4+
// Form services
5+
'XOLoginForm' => 'XOUser\Factory\Form\LoginFormFactory',
6+
'XOSignupForm' => 'XOUser\Factory\Form\SignupFormFactory',
7+
'XOChangePasswordForm' => 'XOUser\Factory\Form\ChangePasswordFormFactory',
8+
9+
// Filter services
10+
'XOLoginFormFilter' => 'XOUser\Factory\Filter\LoginFormFilterFactory',
11+
'XOSignupFormFilter' => 'XOUser\Factory\Filter\SignupFormFilterFactory',
12+
'XOChangePasswordFormFilter' => 'XOUser\Factory\Filter\ChangePasswordFormFilterFactory',
13+
14+
// Mapper services
15+
'XOUserService' => 'XOUser\Factory\Service\UserServiceFactory',
16+
'XOUserMapper' => 'XOUser\Factory\Mapper\UserMapperFactory',
17+
18+
// Session services
19+
'XOSessionConfig' => 'XOUser\Factory\Session\SessionConfigFactory',
20+
'XOSaveHandler' => 'XOUser\Factory\Session\SaveHandlerFactory',
21+
22+
// DB Adapter service
23+
'XODbAdapter' => 'XOUser\Factory\Db\Adapter\DbAdapterFactory',
24+
25+
// Authentication services
26+
'XOAuthAdapter' => 'XOUser\Factory\Authentication\Adapter\AuthAdapterFactory',
27+
'XOAuthStorage' => 'XOUser\Factory\Authentication\Storage\AuthStorageFactory',
28+
'XOAuthService' => 'XOUser\Factory\Authentication\AuthenticationServiceFactory',
29+
),
30+
);

0 commit comments

Comments
 (0)