Completed
Push — 1 ( 9b7a9d...a879de )
by Morven
07:43
created

Ext_Users_Member::sendVerificationEmail()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 3
nop 0
1
<?php
2
3
class Ext_Users_Member extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
        "VerificationCode" => "Varchar(40)"
7
    );
8
9
    private static $has_many = array();
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    public function isVerified()
12
    {
13
        return Permission::checkMember($this->owner, "USERS_VERIFIED");
14
    }
15
16
    /**
17
     * Register a new user account using the provided data
18
     * and then return the current member
19
     *
20
     * @param array $data
21
     * @return Member
22
     */
23
    public function Register($data)
24
    {
25
        // If we have passed a confirm password field, clean the
26
        // data
27
        if (isset($data["Password"]) && is_array($data["Password"]) && isset($data["Password"]["_Password"])) {
28
            $data["Password"] = $data["Password"]["_Password"];
29
        }
30
        
31
        $this->owner->update($data);
32
33
        // Set verification code for this user
34
        $this->owner->VerificationCode = sha1(mt_rand() . mt_rand());
35
        $this->owner->write();
36
37
        // Add member to any groups that have been specified
38
        if (count(Users::config()->new_user_groups)) {
39
            $groups = Group::get()->filter(array(
40
                "Code" => Users::config()->new_user_groups
41
            ));
42
43
            foreach ($groups as $group) {
44
                $group->Members()->add($this->owner);
45
                $group->write();
46
            }
47
        }
48
49
        // Send a verification email, if needed
50
        if (Users::config()->send_verification_email) {
51
            $this->owner->sendVerificationEmail();
52
        }
53
54
        // Login (if enabled)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
        if (Users::config()->login_after_register) {
56
            $this->owner->LogIn(isset($data['Remember']));
57
        }
58
59
        return $this->owner;
60
    }
61
62
    /**
63
     * Send a verification email to this user account
64
     *
65
     * @return boolean
66
     */
67
    public function sendVerificationEmail()
68
    {
69
        if ($this->owner->exists()) {
70
            $controller = Injector::inst()->get("Users_Register_Controller");
71
            $subject = _t("Users.PleaseVerify", "Please verify your account");
72
73
            if (Users::config()->send_email_from) {
74
                $from = Users::config()->send_email_from;
75
            } else {
76
                $from = Email::config()->admin_email;
77
            }
78
79
            $email = Email::create();
80
            $email
81
                ->setFrom($from)
82
                ->setTo($this->owner->Email)
83
                ->setSubject($subject)
84
                ->setTemplate('UsersAccountVerification')
85
                ->populateTemplate(ArrayData::create(array(
86
                    "Link" => Controller::join_links(
87
                        $controller->AbsoluteLink("verify"),
88
                        $this->owner->ID,
89
                        $this->owner->VerificationCode
90
                    )
91
                )));
92
93
            $email->send();
94
95
            return true;
96
        }
97
98
        return false;
99
    }
100
}
101