Class: Lesli::UsersValidator

Inherits:
ApplicationLesliValidator show all
Defined in:
app/validators/lesli/users_validator.rb

Instance Method Summary collapse

Methods inherited from ApplicationLesliValidator

#initialize, #valid?

Constructor Details

This class inherits a constructor from Lesli::ApplicationLesliValidator

Instance Method Details

#password_complexity(password) ⇒ Object

validates if password meet with the minimum password requirements this settings are stored in the account_settings table



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
# File 'app/validators/lesli/users_validator.rb', line 46

def password_complexity(password)

    if password.blank?
        failures.push('error_password_cannot_be_blank') 
        return self
    end

    # remove all the special characters for easy validations of some rules
    password_string_no_special = password.gsub(/[^0-9A-Za-z]/, '')

    # database keys with the parameters to validate
    password_values = [
        'password_expiration_time_days',
        'password_enforce_complexity',
        'password_special_char_count',
        'password_lowercase_count',
        'password_uppercase_count',
        'password_minimum_length',
        'password_digit_count'
    ].map do |setting_name|
        "name = '#{setting_name}'"
    end

    password_settings = []

    # get password settings from the database
    if @resource.

        # if user is already registered
        password_settings = @resource..settings.where(password_values.join(" or "))

    elseif .first

        # for new accounts just take the first account in the database
        password_settings = .first.settings.where(password_values.join(" or "))

    end

    password_settings.each do |settings|

        # exit validations if password complexity is not enabled
        if settings[:name] == 'password_enforce_complexity' and settings[:value] != "1"
            failures = []
            break
        end

        # check if the password has te minimum number of special characters required
        if settings[:name] == 'password_special_char_count'

            # this regex removes all "normal" letters and numbers leaving special characters
            if settings[:value].to_i > password.scan(/[^0-9A-Za-z]/).length
                failures.push('error_password_special_char_count')
            end

        end

        # check if the password has te minimum number of lowercase letters required
        if settings[:name] == 'password_lowercase_count'

            if settings[:value].to_i > password_string_no_special.scan(/[^0-9A-Z]/).length
                failures.push('error_password_lowercase_count')
            end

        end

        # check if the password has te minimum number of uppercase letters required
        if settings[:name] == 'password_uppercase_count'

            if settings[:value].to_i > password_string_no_special.scan(/[^0-9a-z]/).length
                failures.push('error_password_uppercase_count')
            end

        end

        # check if the password has te minium number of numbers required xD
        if settings[:name] == 'password_digit_count'

            if settings[:value].to_i > password_string_no_special.scan(/[^A-Za-z]/).length
                failures.push('error_password_digit_count')
            end

        end

        # check if the password has te minimum size required
        if settings[:name] == 'password_minimum_length'

            if settings[:value].to_i > password.length
                failures.push('error_password_minimum_length')
            end

        end

    end

    return self

end

#validateObject



36
37
38
39
40
41
# File 'app/validators/lesli/users_validator.rb', line 36

def validate 
    active?()
    confirmed?()
    roles_empty?()
    active_roles?()
end