Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace factor_auth;
18
 
19
use stdClass;
20
use tool_mfa\local\factor\object_factor_base;
21
 
22
/**
23
 * Auth factor class.
24
 *
25
 * @package     factor_auth
26
 * @author      Mikhail Golenkov <golenkovm@gmail.com>
27
 * @copyright   Catalyst IT
28
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class factor extends object_factor_base {
31
 
32
    /**
33
     * Auth Factor implementation.
34
     * Factor is a singleton, can only be one instance.
35
     *
36
     * @param stdClass $user the user to check against.
37
     * @return array
38
     */
39
    public function get_all_user_factors(stdClass $user): array {
40
        global $DB;
41
        $records = $DB->get_records('tool_mfa', ['userid' => $user->id, 'factor' => $this->name]);
42
 
43
        if (!empty($records)) {
44
            return $records;
45
        }
46
 
47
        // Null records returned, build new record.
48
        $record = [
49
            'userid' => $user->id,
50
            'factor' => $this->name,
51
            'timecreated' => time(),
52
            'createdfromip' => $user->lastip,
53
            'timemodified' => time(),
54
            'revoked' => 0,
55
        ];
56
        $record['id'] = $DB->insert_record('tool_mfa', $record, true);
57
        return [(object) $record];
58
    }
59
 
60
    /**
61
     * Auth Factor implementation.
62
     * Factor does not have input.
63
     *
64
     * {@inheritDoc}
65
     */
66
    public function has_input(): bool {
67
        return false;
68
    }
69
 
70
    /**
71
     * Auth Factor implementation.
72
     * State check is performed here, as there is no form to do it in.
73
     *
74
     * {@inheritDoc}
75
     */
76
    public function get_state(): string {
77
        global $USER;
78
 
79
        $safetypes = get_config('factor_auth', 'goodauth');
80
        if (strlen($safetypes) != 0) {
81
            $safetypes = explode(',', $safetypes);
82
 
83
            // Check all safetypes against user auth.
84
            if (in_array($USER->auth, $safetypes, true)) {
85
                return \tool_mfa\plugininfo\factor::STATE_PASS;
86
            }
87
            return \tool_mfa\plugininfo\factor::STATE_NEUTRAL;
88
        } else {
89
            return \tool_mfa\plugininfo\factor::STATE_NEUTRAL;
90
        }
91
    }
92
 
93
    /**
94
     * Auth Factor implementation.
95
     * The state can never be set. Always return true.
96
     *
97
     * @param string $state the state constant to set
98
     * @return bool
99
     */
100
    public function set_state(string $state): bool {
101
        return true;
102
    }
103
 
104
    /**
105
     * Auth factor implementation.
106
     * Return list of auth types that are safe.
107
     *
108
     * {@inheritDoc}
109
     */
110
    public function get_summary_condition(): string {
111
        $safetypes = get_config('factor_auth', 'goodauth');
112
 
113
        return get_string('summarycondition', 'factor_'.$this->name, $safetypes);
114
    }
115
}