cop-detective 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac187ef144e420f686140d6b3d31dc6da3b59c19
4
+ data.tar.gz: fcd5e4adc6df5242df3567d573fe672ff90f43f2
5
+ SHA512:
6
+ metadata.gz: 1c0647fa726ad7f1f05a4c29257183efd90939f6640adedc45d3bafa7791c6f419a040ddd226fe83a52d9decbe200771dafa627f42cdd1d20b4b328e85509a01
7
+ data.tar.gz: 89519a82e39d0948ec341e66377ba34fb8300820f09ed6d43ed29d31bb4d57e43f0a6aef760cc564e1209834829fd063d538bff4ce489e0f7dca455c61abbf07
@@ -0,0 +1,47 @@
1
+ require 'active_model'
2
+ require 'active_support'
3
+ require_relative 'errors'
4
+ require_relative 'validator'
5
+
6
+ class CopDetective
7
+ cattr_reader :messages
8
+ class << self
9
+
10
+ include ActiveModel::SecurePassword
11
+ @@messages = CopDetectiveValidator.messages
12
+
13
+ def configure(options)
14
+ if CopDetectiveValidator.options_valid?(options)
15
+ @@old_password = options[:old_password] ||= 'not set'
16
+ @@password = options[:password]
17
+ @@confirmation = options[:confirmation]
18
+ end
19
+ end
20
+
21
+ def update_user(user)
22
+ return validate_new_passwords(user) if valid_credentials?(user, @@old_password)
23
+ messages[:error] = ErrorMessages.unsaved_password(ErrorMessages.invalid_password)
24
+ end
25
+
26
+ def create_user(user)
27
+ if CopDetectiveValidator.new_passwords_match?(@@password, @@confirmation) && user.valid?
28
+ user.save
29
+ @@messages[:notice] = "Account created. You may now log in."
30
+ else
31
+ @@messages[:error] = "Passwords don't match or other params are not valid."
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def validate_new_passwords(user)
38
+ CopDetectiveValidator.validate_new_passwords(user, @@password, @@confirmation)
39
+ end
40
+
41
+ def valid_credentials?(user, old_password)
42
+ CopDetectiveValidator.valid_credentials?(user, old_password)
43
+ end
44
+
45
+ end
46
+
47
+ end
data/lib/errors.rb ADDED
@@ -0,0 +1,25 @@
1
+ class CopDetective
2
+ module ErrorMessages
3
+ class << self
4
+ def unsaved_password(reason)
5
+ "Your new password was not saved. #{reason}"
6
+ end
7
+
8
+ def invalid_password
9
+ "You entered your original password incorrectly."
10
+ end
11
+
12
+ def non_matching
13
+ "Your new passwords don't match."
14
+ end
15
+
16
+ def show_errors(model)
17
+ flash[:error] = model.errors.full_messages
18
+ end
19
+
20
+ def options_error(key, error)
21
+ raise "Option passed to #{key} #{error}"
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/validator.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'active_support'
2
+ class CopDetectiveValidator
3
+ include CopDetective::ErrorMessages
4
+ cattr_accessor :messages
5
+
6
+ class << self
7
+ @@messages = {notice: nil, error: nil}
8
+ @@options_errors = {option_nil: "is nil.",
9
+ empty: "is an empty string."
10
+ }
11
+ def validate_new_passwords(user, password, confirmation)
12
+ if new_passwords_match?(password, confirmation)
13
+ user.password = password
14
+ user.update_attributes(password: password)
15
+ @@messages[:notice] = "Password updated"
16
+ else
17
+ @@messages[:error] = CopDetective::ErrorMessages.unsaved_password(CopDetective::ErrorMessages.non_matching)
18
+ end
19
+ end
20
+
21
+ def options_valid?(options)
22
+ options.each do |k, v|
23
+ return CopDetective::ErrorMessages.options_error(k, @@options_errors[:option_nil]) if v.nil?
24
+ return CopDetective::ErrorMessages.options_error(k, @@options_errors[:empty]) if v.empty?
25
+ end
26
+ true
27
+ end
28
+
29
+ def valid_credentials?(user, old_password)
30
+ user.authenticate(old_password) == user
31
+ end
32
+
33
+ def new_passwords_match?(password, confirmation)
34
+ password == confirmation
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cop-detective
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Kamin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bcrypt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: A simple gem to use in your controller to remove some of the work of
56
+ comparing passwords and confirmations.
57
+ email: jordanakamin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/cop_detective.rb
63
+ - lib/errors.rb
64
+ - lib/validator.rb
65
+ homepage: http://rubygems.org/gems/cop-detective
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.4.5
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Do your passwords match?
89
+ test_files: []