1: <?php
2:
3: App::uses('AuthenticationUser', 'Authentication.Model');
4:
5: class AuthenticationComponent extends Component {
6:
7: public function __construct(\ComponentCollection $collection, $settings = array()) {
8: parent::__construct($collection, $settings + array(
9: 'emailField' => 'email',
10: 'usernameField' => 'username',
11: 'passwordField' => 'password',
12: 'activeField' => 'active',
13: 'userModel' => null,
14: 'loginRedirect' => null,
15: ));
16: AuthenticationUser::configure($this->settings);
17: }
18:
19: public $components = array(
20: 'Auth' => array(
21: 'loginAction' => array(
22: 'controller' => 'authentication',
23: 'action' => 'login',
24: 'plugin' => 'authentication'
25: )
26: )
27: );
28:
29: public function initialize(\Controller $controller) {
30: parent::initialize($controller);
31: if (!$controller->Components->loaded('Auth')) {
32: $controller->Auth = $controller->Components->load('Auth');
33: $controller->Auth->initialize($controller);
34: }
35: $controller->Auth->authenticate = array(
36: 'all' => array(
37: 'userModel' => $this->settings['userModel'],
38: 'fields' => array(
39: 'username' => $this->settings['usernameField']
40: , 'password' => $this->settings['passwordField']
41: ),
42: ),
43: 'Form'
44: );
45: $controller->Auth->authorize = array('Controller');
46: $controller->Auth->loginAction = array(
47: 'controller' => 'authentication',
48: 'action' => 'login',
49: 'plugin' => 'authentication'
50: );
51: $controller->Auth->loginRedirect = $this->settings['loginRedirect'];
52: $controller->Auth->allow();
53: $controller->Auth->allow('index', 'add', 'edit', 'delete', 'view');
54: }
55:
56: public function sendSelfUserCreationNotification($userId, $password) {
57: App::uses('CakeEmail', 'Network/Email');
58:
59: $model = ClassRegistry::init('Authentication.AuthenticationUser');
60: $user = $model->findById($userId);
61: $user = $user[$model->alias];
62:
63: $email = new CakeEmail('default');
64: $email->to($user['email']);
65: $email->subject(__d('authentication','Your account was created.', true));
66: $email->send(
67: __d('authentication','Usernamme') . ': ' . $user['username']
68: . "\n" . __d('authentication','Password') . ': ' . $password);
69: }
70:
71: public function userId() {
72: $user = $this->Auth->user();
73: return $user[$this->_userModel()->primaryKey];
74: }
75:
76: private function _userModel() {
77: return ClassRegistry::init($this->settings['userModel']);
78: }
79:
80: 81: 82: 83:
84: public static function generateRandomPassword($length = 8) {
85:
86:
87: $password = "";
88:
89:
90:
91:
92:
93: $possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
94:
95:
96: $maxlength = strlen($possible);
97:
98:
99: if ($length > $maxlength) {
100: $length = $maxlength;
101: }
102:
103:
104: $i = 0;
105:
106:
107: while ($i < $length) {
108:
109:
110: $char = substr($possible, mt_rand(0, $maxlength - 1), 1);
111:
112:
113: if (!strstr($password, $char)) {
114:
115: $password .= $char;
116:
117: $i++;
118: }
119: }
120:
121:
122: return $password;
123: }
124:
125: }
126:
127: ?>
128: