summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/email.rb31
-rw-r--r--lib/local_email.rb67
-rw-r--r--lib/login_format_validation.rb21
3 files changed, 119 insertions, 0 deletions
diff --git a/lib/email.rb b/lib/email.rb
new file mode 100644
index 0000000..4090275
--- /dev/null
+++ b/lib/email.rb
@@ -0,0 +1,31 @@
+class Email < String
+ include ActiveModel::Validations
+
+ validates :email,
+ :format => {
+ :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, #local part of email is case-sensitive, so allow uppercase letter.
+ :message => "needs to be a valid email address"
+ }
+
+ # Make sure we can call Email.new(nil) and get an invalid email address
+ def initialize(s)
+ super(s.to_s)
+ end
+
+ def to_partial_path
+ "emails/email"
+ end
+
+ def to_param
+ to_s
+ end
+
+ def email
+ self
+ end
+
+ def handle
+ self.split('@').first
+ end
+
+end
diff --git a/lib/local_email.rb b/lib/local_email.rb
new file mode 100644
index 0000000..7c592e1
--- /dev/null
+++ b/lib/local_email.rb
@@ -0,0 +1,67 @@
+require 'email'
+class LocalEmail < Email
+
+ BLACKLIST_FROM_RFC2142 = [
+ 'postmaster', 'hostmaster', 'domainadmin', 'webmaster', 'www',
+ 'abuse', 'noc', 'security', 'usenet', 'news', 'uucp',
+ 'ftp', 'sales', 'marketing', 'support', 'info'
+ ]
+
+ def self.domain
+ APP_CONFIG[:domain]
+ end
+
+ validates :email,
+ :format => {
+ :with => /@#{domain}\Z/i,
+ :message => "needs to end in @#{domain}"
+ }
+
+ validate :handle_allowed
+
+ def initialize(s)
+ super
+ append_domain_if_needed
+ end
+
+ def to_key
+ [handle]
+ end
+
+ def domain
+ LocalEmail.domain
+ end
+
+ protected
+
+ def append_domain_if_needed
+ unless self.index('@')
+ self << '@' + domain
+ end
+ end
+
+ def handle_allowed
+ errors.add(:handle, "is reserved.") if handle_reserved?
+ end
+
+ def handle_reserved?
+ # *ARRAY in a case statement tests if ARRAY includes the handle.
+ case handle
+ when *APP_CONFIG[:handle_blacklist]
+ true
+ when *APP_CONFIG[:handle_whitelist]
+ false
+ when *BLACKLIST_FROM_RFC2142
+ true
+ else
+ handle_in_passwd?
+ end
+ end
+
+ def handle_in_passwd?
+ Etc.getpwnam(handle).present?
+ rescue ArgumentError
+ # handle was not found
+ return false
+ end
+end
diff --git a/lib/login_format_validation.rb b/lib/login_format_validation.rb
new file mode 100644
index 0000000..c1fcf70
--- /dev/null
+++ b/lib/login_format_validation.rb
@@ -0,0 +1,21 @@
+module LoginFormatValidation
+ extend ActiveSupport::Concern
+
+ #TODO: Probably will replace this. Playing with using it for aliases too, but won't want it connected to login field.
+
+ included do
+ # Have multiple regular expression validations so we can get specific error messages:
+ validates :login,
+ :format => { :with => /\A.{2,}\z/,
+ :message => "Must have at least two characters"}
+ validates :login,
+ :format => { :with => /\A[a-z\d_\.-]+\z/,
+ :message => "Only lowercase letters, digits, . - and _ allowed."}
+ validates :login,
+ :format => { :with => /\A[a-z].*\z/,
+ :message => "Must begin with a lowercase letter"}
+ validates :login,
+ :format => { :with => /\A.*[a-z\d]\z/,
+ :message => "Must end with a letter or digit"}
+ end
+end