Completed
Push — master ( ee79b5...34b090 )
by Stefan
04:54
created

User::__construct()   D

Complexity

Conditions 10
Paths 9

Size

Total Lines 44
Code Lines 30

Duplication

Lines 12
Ratio 27.27 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 30
c 3
b 0
f 0
nc 9
nop 1
dl 12
loc 44
rs 4.8196

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* * ********************************************************************************
4
 * (c) 2011-15 GÉANT on behalf of the GN3, GN3plus and GN4 consortia
5
 * License: see the LICENSE file in the root directory
6
 * ********************************************************************************* */
7
?>
8
<?php
9
10
/**
11
 * This class manages user privileges and bindings to institutions
12
 *
13
 * @author Stefan Winter <[email protected]>
14
 * @author Tomasz Wolniewicz <[email protected]>
15
 * 
16
 * @package Developer
17
 */
18
/**
19
 * necessary includes
20
 */
21
require_once('DBConnection.php');
22
require_once("Federation.php");
23
require_once("IdP.php");
24
require_once('EntityWithDBProperties.php');
25
require_once("core/PHPMailer/src/PHPMailer.php");
26
require_once("core/PHPMailer/src/SMTP.php");
27
28
/**
29
 * This class represents a known CAT User (i.e. an institution and/or federation adiministrator).
30
 * @author Stefan Winter <[email protected]>
31
 * 
32
 * @package Developer
33
 */
34
class User extends EntityWithDBProperties {
35
36
    /**
37
     * Class constructor. The required argument is a user's persistent identifier as was returned by the authentication source.
38
     * 
39
     * @param string $user_id User Identifier as per authentication source
40
     */
41
    public function __construct($user_id) {
42
        $this->databaseType = "USER";
43
        $this->attributes = [];
44
        $this->entityOptionTable = "user_options";
45
        $this->entityIdColumn = "user_id";
46
        $this->identifier = DBConnection::escape_value($this->databaseType, $user_id);
47
48
        $optioninstance = Options::instance();
49
50
        if (Config::$CONSORTIUM['name'] == "eduroam" && isset(Config::$CONSORTIUM['deployment-voodoo']) && Config::$CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED
51
// e d u r o a m DB doesn't follow the usual approach
52
// we could get multiple rows below (if administering multiple
53
// federations), so consolidate all into the usual options
54
            $info = DBConnection::exec($this->databaseType, "SELECT email, common_name, role, realm FROM view_admin WHERE eptid = '$user_id'");
55
            $visited = FALSE;
56
            while ($a = mysqli_fetch_object($info)) {
57
                if (!$visited) {
58
                    $mailOptinfo = $optioninstance->optionType("user:email");
59
                    $this->attributes[] = ["name" => "user:email", "value" => $a->email, "level" => "User", "row" => 0, "flag" => $mailOptinfo['flag']];
60
                    $realnameOptinfo = $optioninstance->optionType("user:realname");
61
                    $this->attributes[] = ["name" => "user:realname", "value" => $a->common_name, "level" => "User", "row" => 0, "flag" => $realnameOptinfo['flag']];
62
                    $visited = TRUE;
63
                }
64
                if ($a->role == "fedadmin") {
65
                    $optinfo = $optioninstance->optionType("user:fedadmin");
66
                    $this->attributes[] = ["name" => "user:fedadmin", "value" => strtoupper($a->realm), "level" => "User", "row" => 0, "flag" => $optinfo['flag']];
67
                }
68
            }
69
        } else {
70
            $user_options = DBConnection::exec($this->databaseType, "SELECT option_name, option_value, id AS row FROM user_options WHERE user_id = '$user_id'");
71 View Code Duplication
            while ($a = mysqli_fetch_object($user_options)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
// decode base64 for files (respecting multi-lang)
73
                $optinfo = $optioninstance->optionType($a->option_name);
74
                $flag = $optinfo['flag'];
75
76
                if ($optinfo['type'] != "file") {
77
                    $this->attributes[] = ["name" => $a->option_name, "value" => $a->option_value, "level" => "User", "row" => $a->row, "flag" => $flag];
78
                } else {
79
                    $decodedAttribute = $this->decodeFileAttribute($a->option_value);
80
                    $this->attributes[] = ["name" => $a->option_name, "value" => ($decodedAttribute['lang'] == "" ? $decodedAttribute['content'] : serialize($decodedAttribute)), "level" => "User", "row" => $a->row, "flag" => $flag];
81
                }
82
            }
83
        }
84
    }
85
86
    /**
87
     * This function checks whether a user is a federation administrator. When called without argument, it only checks if the
88
     * user is a federation administrator of *any* federation. When given a parameter (ISO shortname of federation), it checks
89
     * if the user administers this particular federation.
90
     * 
91
     * @param string $federation optional: federation to be checked
92
     * @return boolean TRUE if the user is federation admin, FALSE if not 
93
     */
94
    public function isFederationAdmin($federation = 0) {
95
        $feds = $this->getAttributes("user:fedadmin");
96
        if (count($feds) == 0) { // not a fedadmin at all
97
            return FALSE;
98
        }
99
        if ($federation === 0) { // fedadmin for one; that's all we want to know
100
            return TRUE;
101
        }
102
        foreach ($feds as $fed) { // check if authz is for requested federation
103
            if (strtoupper($fed['value']) == strtoupper($federation)) {
104
                return TRUE;
105
            }
106
        }
107
        return FALSE; // no luck so far? Not the admin we are looking for.
108
    }
109
110
    /**
111
     * This function tests if the current user has been configured as the system superadmin, i.e. if the user is allowed
112
     * to execute the 112365365321.php script
113
     *
114
     * @return boolean TRUE if the user is a superadmin, FALSE if not 
115
     */
116
    public function isSuperadmin() {
117
        return in_array($this->identifier, Config::$SUPERADMINS);
118
    }
119
120
    /**
121
     *  This function tests if the current user is an ovner of a given IdP
122
     *
123
     * @return boolean TRUE if the user is an owner, FALSE if not 
124
     */
125
    public function isIdPOwner($idp) {
126
        $temp = new IdP($idp);
127
        foreach ($temp->owner() as $oneowner) {
128
            if ($oneowner['ID'] == $this->identifier) {
129
                return TRUE;
130
            }
131
        }
132
        return FALSE;
133
    }
134
135
    public function sendMailToUser($subject, $content) {
136
        $mailaddr = $this->getAttributes("user:email");
137
        if (count($mailaddr) == 0) { // we don't know user's mail address
138
            return FALSE;
139
        }
140
// use PHPMailer to send the mail
141
        $mail = new PHPMailer\PHPMailer\PHPMailer();
142
        $mail->isSMTP();
143
        $mail->SMTPAuth = true;
144
        $mail->Port = 587;
145
        $mail->SMTPSecure = 'tls';
146
        $mail->Host = Config::$MAILSETTINGS['host'];
147
        $mail->Username = Config::$MAILSETTINGS['user'];
148
        $mail->Password = Config::$MAILSETTINGS['pass'];
149
// formatting nitty-gritty
150
        $mail->WordWrap = 72;
151
        $mail->isHTML(FALSE);
152
        $mail->CharSet = 'UTF-8';
153
// who to whom?
154
        $mail->From = Config::$APPEARANCE['from-mail'];
155
        $mail->FromName = Config::$APPEARANCE['productname'] . " Notification System";
156
        $mail->addReplyTo(Config::$APPEARANCE['support-contact']['mail'], Config::$APPEARANCE['productname'] . " " . _("Feedback"));
157
        $mail->addAddress($mailaddr[0]["value"]);
158
// what do we want to say?
159
        $mail->Subject = $subject;
160
        $mail->Body = $content;
161 View Code Duplication
        if (isset(Config::$CONSORTIUM['certfilename'], Config::$CONSORTIUM['keyfilename'], Config::$CONSORTIUM['keypass'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
            $mail->sign(Config::$CONSORTIUM['certfilename'], Config::$CONSORTIUM['keyfilename'], Config::$CONSORTIUM['keypass']);
163
        }
164
165
        $sent = $mail->send();
166
167
        return $sent;
168
    }
169
170
}
171