-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_login.module
183 lines (146 loc) · 4.81 KB
/
code_login.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @file
* Simple One-input login form with Code generator.
*/
define('CODE_LOGIN_ADMIN_PATH', 'admin/config/people/code-login');
define('CODE_LOGIN_TABLE', 'login_codes');
/**
* Implements hook_init().
*/
function code_login_init() {
$types = array(
1 => '1 Week',
2 => '2 Weeks',
3 => '1 Month',
4 => '3 Months',
5 => '6 Months',
6 => '1 Year',
7 => 'Unlimited',
);
variable_set('code_login_defined_types', $types);
}
/**
* Implements hook_menu().
*/
function code_login_menu() {
$login_url = variable_get('custom_login_page_path') ? variable_get('custom_login_page_path') : 'code-login';
$items['admin/code-login'] = array(
'title' => 'Code Logins',
'description' => 'Administer Login Codes',
'page callback' => 'drupal_get_form',
'page arguments' => array('code_login_form'),
'access arguments' => array('administer code login'),
'type' => MENU_NORMAL_ITEM,
'weight' => 0,
'file' => 'code_login.admin.inc',
);
$items[$login_url] = array(
'title callback' => 'custom_login_title_callback',
'page callback' => 'drupal_get_form',
'page arguments' => array('code_custom_login_form'),
'access callback' => 'user_is_anonymous',
'type' => MENU_CALLBACK,
'file' => 'code_login.pages.inc',
);
$items['admin/config/people/code-login/%/edit'] = array(
'title callback' => 'code_login_edit_title_callback',
'title arguments' => array(4),
'page callback' => 'drupal_get_form',
'page arguments' => array('code_login_edit_form', 4),
'access arguments' => array('administer code login'),
'type' => MENU_CALLBACK,
'file' => 'code_login.admin.inc',
);
$items['admin/config/people/code-login/%/delete'] = array(
'title callback' => 'code_login_delete_title_callback',
'title arguments' => array(4),
'page callback' => 'drupal_get_form',
'page arguments' => array('code_login_delete_form', 4),
'access arguments' => array('administer code login'),
'type' => MENU_CALLBACK,
'file' => 'code_login.admin.inc',
);
return $items;
}
function custom_login_title_callback() {
$title = variable_get('custom_login_page_title') ? variable_get('custom_login_page_title') : 'Custom Login';
return $title;
}
function code_login_edit_title_callback($code_id) {
$title = 'Edit code ' . $code_id;
return $title;
}
function code_login_delete_title_callback($code_id) {
$title = 'Delete code ' . $code_id;
return $title;
}
function code_login_load($code_id) {
$query = db_select('login_codes', 'c');
$query->condition('cid', $code_id, '=');
$query->join('users', 'u', 'c.uid = u.uid');
$query
->fields('c', array('cid', 'uid', 'code', 'code_type', 'creation', 'first_login', 'expiration'))
->fields('u', array('name', 'created', 'mail', 'access', 'login', 'data'))
->orderBy('u.created', 'DESC');
return $query->execute()->fetchObject();
}
function code_login_update($record) {
drupal_write_record(CODE_LOGIN_TABLE, $record, 'cid');
}
function code_login_user_custom_delete($code_id) {
$code = code_login_load($code_id);
db_delete('login_codes')
->condition('cid', $code_id, '=')
->execute();
$uid = $code->uid;
user_delete($uid);
}
function code_login_user_delete($account) {
db_delete('login_codes')
->condition('uid', $account->uid)
->execute();
}
function code_login_user_login(&$edit, $account) {
//Update login_codes table with first login and expiration
$code_user = db_select('login_codes', 'c')
->fields('c', array('uid', 'code_type'))
->condition('uid', $account->uid, '=')
->execute()
->fetchAll();
$types = array(
1 => '1 week',
2 => '2 weeks',
3 => '1 month',
4 => '3 months',
5 => '6 months',
6 => '1 year',
7 => '20 years',
);
if (!empty($code_user)) {
if ($account->access == 0) {
$date = date_create(date('d-m-Y',$account->login));
date_add($date, date_interval_create_from_date_string($types[$code_user[0]->code_type]));
$hour = date('H',$account->login);
$min = date('i',$account->login);
$sec = date('s',$account->login);
date_time_set($date, $hour, $min, $sec);
$formatted_date = date_format($date, 'd-m-Y H:i:s');
$expiration = strtotime($formatted_date);
$row_update = db_update('login_codes')
->fields(array('first_login' => $account->login, 'expiration' => $expiration))
->condition('uid', $account->uid , '=')
->execute();
}
}
}
/**
* Implements hook_permission().
*/
function code_login_permission() {
return array(
'administer code login' => array(
'title' => t('Administer Code Login'),
)
);
}