Skip to content

Commit d0c97dd

Browse files
committed
Adição de página para trocar CPF
1 parent c2ed834 commit d0c97dd

File tree

6 files changed

+376
-7
lines changed

6 files changed

+376
-7
lines changed

auth/db/auth.php

+18
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ function user_update_password($user, $newpassword) {
267267
}
268268
}
269269

270+
function user_update_cpf($user, $newcpf) {
271+
global $DB;
272+
273+
if ($this->is_internal()) {
274+
$puser = $DB->get_record('user_info_data', array('id'=>$user->id), '*', MUST_EXIST);
275+
276+
if (update_internal_user_cpf($puser, $newcpf)) {
277+
$user->data = $puser->data;
278+
return true;
279+
} else {
280+
return false;
281+
}
282+
} else {
283+
// We should have never been called!
284+
return false;
285+
}
286+
}
287+
270288
/**
271289
* Synchronizes user from external db to moodle user table.
272290
*

auth/email/auth.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ function user_update_password($user, $newpassword) {
8282
// legacy md5 algorithm).
8383
return update_internal_user_password($user, $newpassword);
8484
}
85-
85+
function user_update_cpf($user, $newcpf) {
86+
$user = get_complete_user_data('id', $user->id);
87+
88+
return update_internal_user_cpf($user, $newcpf);
89+
}
8690
function can_signup() {
8791
return true;
8892
}

lib/moodlelib.php

+89-6
Original file line numberDiff line numberDiff line change
@@ -2209,9 +2209,37 @@ function format_time($totalsecs, $str = null) {
22092209
* @return string the formatted date/time.
22102210
*/
22112211
function userdate($date, $format = '', $timezone = 99, $fixday = true, $fixhour = true) {
2212-
$calendartype = \core_calendar\type_factory::get_calendar_instance();
2213-
return $calendartype->timestamp_to_date_string($date, $format, $timezone, $fixday, $fixhour);
2214-
}
2212+
$calendartype = \core_calendar\type_factory::get_calendar_instance();
2213+
return trocaMes($calendartype->timestamp_to_date_string($date, $format, $timezone, $fixday, $fixhour));
2214+
}
2215+
2216+
function trocaMes($data) {
2217+
$dataPt = $data;
2218+
$dataPt = str_replace("January", "Janeiro", $dataPt);
2219+
$dataPt = str_replace("February", "Fevereiro", $dataPt);
2220+
$dataPt = str_replace("March", "Março", $dataPt);
2221+
$dataPt = str_replace("April", "Abril", $dataPt);
2222+
$dataPt = str_replace("May", "Maio", $dataPt);
2223+
$dataPt = str_replace("June", "Junho", $dataPt);
2224+
$dataPt = str_replace("July", "Julho", $dataPt);
2225+
$dataPt = str_replace("August", "Agosto", $dataPt);
2226+
$dataPt = str_replace("September", "Setembro", $dataPt);
2227+
$dataPt = str_replace("October", "Outubro", $dataPt);
2228+
$dataPt = str_replace("November", "Novembro", $dataPt);
2229+
$dataPt = str_replace("December", "Dezembro", $dataPt);
2230+
2231+
$dataPt = str_replace("Sunday", "Domingo", $dataPt);
2232+
$dataPt = str_replace("Monday", "Segunda-feira", $dataPt);
2233+
$dataPt = str_replace("Tuesday", "Terça-feira", $dataPt);
2234+
$dataPt = str_replace("Thursday", "Quinta-feira", $dataPt);
2235+
$dataPt = str_replace("Friday", "Sexta-feira", $dataPt);
2236+
$dataPt = str_replace("Wednesday", "Quarta-feira", $dataPt);
2237+
$dataPt = str_replace("Saturday", "Sábado", $dataPt);
2238+
2239+
2240+
return $dataPt;
2241+
}
2242+
22152243

22162244
/**
22172245
* Returns a formatted date ensuring it is UTF-8.
@@ -2636,6 +2664,30 @@ function require_login($courseorid = null, $autologinguest = true, $cm = null, $
26362664
}
26372665
}
26382666

2667+
2668+
// Verifica se o usuário tem que trocar o CPF.
2669+
if (get_user_preferences('auth_forcecpfchange') && !\core\session\manager::is_loggedinas()) {
2670+
$userauth = get_auth_plugin($USER->auth);
2671+
if (!$preventredirect) {
2672+
if ($setwantsurltome) {
2673+
$SESSION->wantsurl = qualified_me();
2674+
}
2675+
if ($changeurl = $userauth->change_password_url()) {
2676+
// Use plugin custom url.
2677+
redirect($changeurl);
2678+
} else {
2679+
// Use moodle internal method.
2680+
if (empty($CFG->loginhttps)) {
2681+
redirect($CFG->wwwroot .'/login/change_cpf.php');
2682+
} else {
2683+
$wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
2684+
redirect($wwwroot .'/login/change_cpf.php');
2685+
}
2686+
}
2687+
}
2688+
}
2689+
2690+
26392691
// Check whether the user should be changing password (but only if it is REALLY them).
26402692
if (get_user_preferences('auth_forcepasswordchange') && !\core\session\manager::is_loggedinas()) {
26412693
$userauth = get_auth_plugin($USER->auth);
@@ -2862,8 +2914,8 @@ function require_login($courseorid = null, $autologinguest = true, $cm = null, $
28622914
}
28632915
}
28642916
}
2865-
2866-
if (!$access) {
2917+
// MGBF: evita redirecionamento em caso de estar logado como outro usuário
2918+
if (!$access && !\core\session\manager::is_loggedinas()) {
28672919
if ($preventredirect) {
28682920
throw new require_login_exception('Not enrolled');
28692921
}
@@ -4442,6 +4494,13 @@ function complete_user_login($user) {
44424494
// Select password change url.
44434495
$userauth = get_auth_plugin($USER->auth);
44444496

4497+
// Verifica se o usuário tem que trocar o CPF.
4498+
if (get_user_preferences('auth_forcecpfchange', false)) {
4499+
require_once($CFG->dirroot . '/login/lib.php');
4500+
$SESSION->wantsurl = core_login_get_return_url();
4501+
redirect($CFG->httpswwwroot.'/login/change_cpf.php');
4502+
}
4503+
44454504
// Check whether the user should be changing password.
44464505
if (get_user_preferences('auth_forcepasswordchange', false)) {
44474506
if ($userauth->can_change_password()) {
@@ -4455,7 +4514,7 @@ function complete_user_login($user) {
44554514
} else {
44564515
print_error('nopasswordchangeforced', 'auth');
44574516
}
4458-
}
4517+
}
44594518
return $USER;
44604519
}
44614520

@@ -4634,6 +4693,30 @@ function update_internal_user_password($user, $password, $fasthash = false) {
46344693
* @param int $mnethostid
46354694
* @return mixed False, or A {@link $USER} object.
46364695
*/
4696+
4697+
4698+
function update_internal_user_cpf($user, $cpf) {
4699+
global $CFG, $DB;
4700+
// $DB->set_field('user_info_data', 'data', $cpf, array('userid' => $user->id, 'fieldid' => 8));
4701+
4702+
$cpfdata = new stdClass();
4703+
$cpfdata->userid = $user->id;
4704+
$cpfdata->fieldid = 8;
4705+
$cpfdata->data = $cpf;
4706+
$DB->insert_record('user_info_data', $cpfdata);
4707+
4708+
$cpfuser = new stdClass();
4709+
$cpfuser->id = $user->id;
4710+
$cpfuser->username = $cpf;
4711+
$DB->update_record('user', $cpfuser);
4712+
4713+
return true;
4714+
}
4715+
4716+
4717+
4718+
4719+
46374720
function get_complete_user_data($field, $value, $mnethostid = null) {
46384721
global $CFG, $DB;
46394722

login/change_cpf.php

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
// This file is part of Moodle - http://moodle.org/
4+
//
5+
// Moodle is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Moodle is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17+
18+
/**
19+
* Change CPF page.
20+
*
21+
* @package core
22+
* @subpackage auth
23+
* @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*/
26+
27+
require('../config.php');
28+
require_once($CFG->dirroot.'/user/lib.php');
29+
require_once('change_cpf_form.php');
30+
require_once($CFG->libdir.'/authlib.php');
31+
require_once($CFG->dirroot.'/webservice/lib.php');
32+
33+
$id = optional_param('id', SITEID, PARAM_INT); // current course
34+
$return = optional_param('return', 0, PARAM_BOOL); // redirect after password change
35+
36+
$systemcontext = context_system::instance();
37+
38+
//HTTPS is required in this page when $CFG->loginhttps enabled
39+
$PAGE->https_required();
40+
41+
$PAGE->set_url('/login/change_cpf.php', array('id'=>$id));
42+
43+
$PAGE->set_context($systemcontext);
44+
45+
if ($return) {
46+
// this redirect prevents security warning because https can not POST to http pages
47+
if (empty($SESSION->wantsurl)
48+
or stripos(str_replace('https://', 'http://', $SESSION->wantsurl), str_replace('https://', 'http://', $CFG->wwwroot.'/login/change_cof.php')) === 0) {
49+
$returnto = "$CFG->wwwroot/user/preferences.php?userid=$USER->id&course=$id";
50+
} else {
51+
$returnto = $SESSION->wantsurl;
52+
}
53+
unset($SESSION->wantsurl);
54+
55+
redirect($returnto);
56+
}
57+
58+
$strparticipants = get_string('participants');
59+
60+
if (!$course = $DB->get_record('course', array('id'=>$id))) {
61+
print_error('invalidcourseid');
62+
}
63+
64+
// require proper login; guest user can not change cpf
65+
if (!isloggedin() or isguestuser()) {
66+
if (empty($SESSION->wantsurl)) {
67+
$SESSION->wantsurl = $CFG->httpswwwroot.'/login/change_cpf.php';
68+
}
69+
redirect(get_login_url());
70+
}
71+
72+
$PAGE->set_context(context_user::instance($USER->id));
73+
$PAGE->set_pagelayout('admin');
74+
$PAGE->set_course($course);
75+
76+
// do not require change own password cap if change forced
77+
if (!get_user_preferences('auth_forcecpfchange', false)) {
78+
if(!user_can_change_cpf($USER->id))
79+
redirect($CFG->wwwroot);
80+
}
81+
82+
// do not allow "Logged in as" users to change any passwords
83+
if (\core\session\manager::is_loggedinas()) {
84+
print_error('cannotcallscript');
85+
}
86+
87+
// load the appropriate auth plugin
88+
$userauth = get_auth_plugin($USER->auth);
89+
90+
91+
if ($changeurl = $userauth->change_password_url()) {
92+
// this internal scrip not used
93+
redirect($changeurl);
94+
}
95+
96+
$mform = new login_change_cpf_form();
97+
$mform->set_data(array('id'=>$course->id));
98+
99+
$navlinks = array();
100+
$navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
101+
102+
if ($mform->is_cancelled()) {
103+
redirect($CFG->wwwroot.'/user/preferences.php?userid='.$USER->id.'&amp;course='.$course->id);
104+
} else if ($data = $mform->get_data()) {
105+
106+
if (!$userauth->user_update_cpf($USER, $data->newcpf)) {
107+
print_error('errorpasswordupdate', 'auth');
108+
}
109+
110+
// Reset login lockout - we want to prevent any accidental confusion here.
111+
login_unlock_account($USER);
112+
113+
// register success changing password
114+
unset_user_preference('auth_forcecpfchange', $USER);
115+
116+
$strpasswordchanged = 'Seu CPF foi cadastrado com sucesso!';
117+
118+
$fullname = fullname($USER, true);
119+
120+
$PAGE->set_title($strpasswordchanged);
121+
$PAGE->set_heading(fullname($USER));
122+
echo $OUTPUT->header();
123+
124+
notice($strpasswordchanged, new moodle_url($PAGE->url, array('return'=>1)));
125+
126+
echo $OUTPUT->footer();
127+
exit;
128+
}
129+
130+
// make sure we really are on the https page when https login required
131+
$PAGE->verify_https_required();
132+
133+
$strchangepassword = 'Informe seu CPF';
134+
135+
$fullname = fullname($USER, true);
136+
137+
$PAGE->set_title($strchangepassword);
138+
$PAGE->set_heading($fullname);
139+
echo $OUTPUT->header();
140+
141+
if (get_user_preferences('auth_forcecpfchange')) {
142+
echo $OUTPUT->notification('Você deve cadastrar seu CPF antes de continuar.');
143+
}
144+
$mform->display();
145+
echo $OUTPUT->footer();

0 commit comments

Comments
 (0)