From 4a071ef1b33525fa2d1052aa7f21b549447fe767 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 18 Jul 2013 11:31:25 +0200 Subject: move identity creation into user class It's always based on a user and most default values are based on user properties. --- users/app/models/user.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 413b4ac..dda5a41 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -73,6 +73,19 @@ class User < CouchRest::Model::Base alias_method :find_by_param, :find end + def create_identity(attribs = {}, &block) + build_identity(attribs, &block) + identity.save + identity + end + + def build_identity(attribs = {}, &block) + attribs.reverse_merge! user_id: self.id, + address: self.email_address, + destination: self.email_address + Identity.new(attribs, &block) + end + def to_param self.id end -- cgit v1.2.3 From 0acace58c31c96fc1f8836167aeb4f204f72617f Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 18 Jul 2013 17:17:36 +0200 Subject: allow available and unique forwards only --- users/app/models/user.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index dda5a41..5707f24 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -74,7 +74,7 @@ class User < CouchRest::Model::Base end def create_identity(attribs = {}, &block) - build_identity(attribs, &block) + identity = build_identity(attribs, &block) identity.save identity end @@ -135,12 +135,10 @@ class User < CouchRest::Model::Base ## def login_is_unique_alias - has_alias = User.find_by_login_or_alias(username) + alias_identity = Identity.find_by_address(self.email_address) return if has_alias.nil? - if has_alias != self + if alias_identity.user != self errors.add(:login, "has already been taken") - elsif has_alias.login != self.login - errors.add(:login, "may not be the same as one of your aliases") end end -- cgit v1.2.3 From 495c97fbf400ed44a1e64692f2c04d2155a326e7 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 18 Jul 2013 18:09:12 +0200 Subject: remove the remainders of email aliases and forward from user --- users/app/models/user.rb | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 5707f24..62fb7ec 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -6,9 +6,6 @@ class User < CouchRest::Model::Base property :password_verifier, String, :accessible => true property :password_salt, String, :accessible => true - property :email_forward, String, :accessible => true - property :email_aliases, [LocalEmail] - property :public_key, :accessible => true property :enabled, TrueClass, :default => true @@ -43,10 +40,6 @@ class User < CouchRest::Model::Base :confirmation => true, :format => { :with => /.{8}.*/, :message => "needs to be at least 8 characters long" } - validates :email_forward, - :allow_blank => true, - :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} - timestamps! design do @@ -54,19 +47,6 @@ class User < CouchRest::Model::Base load_views(own_path.join('..', 'designs', 'user')) view :by_login view :by_created_at - view :pgp_key_by_handle, - map: <<-EOJS - function(doc) { - if (doc.type != 'User') { - return; - } - emit(doc.login, doc.public_key); - doc.email_aliases.forEach(function(alias){ - emit(alias.username, doc.public_key); - }); - } - EOJS - end # end of design class << self @@ -118,12 +98,6 @@ class User < CouchRest::Model::Base APP_CONFIG['admins'].include? self.login end - # this currently only adds the first email address submitted. - # All the ui needs for now. - def email_aliases_attributes=(attrs) - email_aliases.build(attrs.values.first) if attrs - end - def most_recent_tickets(count=3) Ticket.for_user(self).limit(count).all #defaults to having most recent updated first end @@ -136,7 +110,7 @@ class User < CouchRest::Model::Base def login_is_unique_alias alias_identity = Identity.find_by_address(self.email_address) - return if has_alias.nil? + return if alias_identity.blank? if alias_identity.user != self errors.add(:login, "has already been taken") end -- cgit v1.2.3 From dcaf6232dbd1131cdbd12ac5c2ef997979fd01ff Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 19 Jul 2013 09:50:02 +0200 Subject: add keys to identity --- users/app/models/user.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 62fb7ec..fb7f0aa 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -62,7 +62,8 @@ class User < CouchRest::Model::Base def build_identity(attribs = {}, &block) attribs.reverse_merge! user_id: self.id, address: self.email_address, - destination: self.email_address + destination: self.email_address, + keys: {} Identity.new(attribs, &block) end -- cgit v1.2.3 From 8ddbaa6184e4dbcc6ef7e81cf555cc18d3822dce Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 19 Jul 2013 11:09:25 +0200 Subject: support deprecated API to set users main identity pgp key We'll want to get rid of the #public_key and #public_key= functions but they are still used from the users controller. We'll probably have an identity controller instead at some point. --- users/app/models/user.rb | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index fb7f0aa..546d571 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -6,8 +6,6 @@ class User < CouchRest::Model::Base property :password_verifier, String, :accessible => true property :password_salt, String, :accessible => true - property :public_key, :accessible => true - property :enabled, TrueClass, :default => true validates :login, :password_salt, :password_verifier, @@ -49,14 +47,50 @@ class User < CouchRest::Model::Base view :by_created_at end # end of design + # We proxy access to the pgp_key. So we need to make sure + # the updated identity actually gets saved. + def save(*args) + super + identity.user_id ||= self.id + identity.save if identity.changed? + end + + # So far this only works for creating a new user. + # TODO: Create an alias for the old login when changing the login + def login=(value) + write_attribute 'login', value + @identity = build_identity + end + + # DEPRECATED + # + # Please access identity.keys[:pgp] directly + def public_key=(value) + identity.keys[:pgp] = value + end + + # DEPRECATED + # + # Please access identity.keys[:pgp] directly + def public_key + identity.keys[:pgp] + end + class << self alias_method :find_by_param, :find end + # this is the main identity. login@domain.tld + # aliases and forwards are represented in other identities. + def identity + @identity ||= + Identity.find_by_address_and_destination([email_address, email_address]) + end + def create_identity(attribs = {}, &block) - identity = build_identity(attribs, &block) - identity.save - identity + new_identity = build_identity(attribs, &block) + new_identity.save + new_identity end def build_identity(attribs = {}, &block) -- cgit v1.2.3 From c0b88d9e8fe574d6164f48211db50f3b8a4c4d93 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 19 Jul 2013 12:21:40 +0200 Subject: setter for keys for dirty tracking, more robust tests Just altering identity.keys did not mark identities as changed. Also we now have a sane default for keys. --- users/app/models/user.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 546d571..c791069 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -59,14 +59,19 @@ class User < CouchRest::Model::Base # TODO: Create an alias for the old login when changing the login def login=(value) write_attribute 'login', value - @identity = build_identity + if @identity + @identity.address = email_address + @identity.destination = email_address + else + build_identity + end end # DEPRECATED # - # Please access identity.keys[:pgp] directly + # Please set the key on the identity directly def public_key=(value) - identity.keys[:pgp] = value + identity.set_key(:pgp, value) end # DEPRECATED @@ -83,8 +88,7 @@ class User < CouchRest::Model::Base # this is the main identity. login@domain.tld # aliases and forwards are represented in other identities. def identity - @identity ||= - Identity.find_by_address_and_destination([email_address, email_address]) + @identity ||= find_identity || build_identity end def create_identity(attribs = {}, &block) @@ -96,8 +100,7 @@ class User < CouchRest::Model::Base def build_identity(attribs = {}, &block) attribs.reverse_merge! user_id: self.id, address: self.email_address, - destination: self.email_address, - keys: {} + destination: self.email_address Identity.new(attribs, &block) end @@ -139,6 +142,10 @@ class User < CouchRest::Model::Base protected + def find_identity + Identity.find_by_address_and_destination([email_address, email_address]) + end + ## # Validation Functions ## -- cgit v1.2.3 From d70c5796a2989b7b43f7e287899fb1394ae28280 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 19 Jul 2013 16:02:02 +0200 Subject: separate signup and settings service objects for user --- users/app/models/user.rb | 57 ------------------------------------------------ 1 file changed, 57 deletions(-) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index c791069..f78f290 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -47,63 +47,10 @@ class User < CouchRest::Model::Base view :by_created_at end # end of design - # We proxy access to the pgp_key. So we need to make sure - # the updated identity actually gets saved. - def save(*args) - super - identity.user_id ||= self.id - identity.save if identity.changed? - end - - # So far this only works for creating a new user. - # TODO: Create an alias for the old login when changing the login - def login=(value) - write_attribute 'login', value - if @identity - @identity.address = email_address - @identity.destination = email_address - else - build_identity - end - end - - # DEPRECATED - # - # Please set the key on the identity directly - def public_key=(value) - identity.set_key(:pgp, value) - end - - # DEPRECATED - # - # Please access identity.keys[:pgp] directly - def public_key - identity.keys[:pgp] - end - class << self alias_method :find_by_param, :find end - # this is the main identity. login@domain.tld - # aliases and forwards are represented in other identities. - def identity - @identity ||= find_identity || build_identity - end - - def create_identity(attribs = {}, &block) - new_identity = build_identity(attribs, &block) - new_identity.save - new_identity - end - - def build_identity(attribs = {}, &block) - attribs.reverse_merge! user_id: self.id, - address: self.email_address, - destination: self.email_address - Identity.new(attribs, &block) - end - def to_param self.id end @@ -142,10 +89,6 @@ class User < CouchRest::Model::Base protected - def find_identity - Identity.find_by_address_and_destination([email_address, email_address]) - end - ## # Validation Functions ## -- cgit v1.2.3 From 370be6caa8a366774fba15d09fb03ee4d923b861 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 24 Jul 2013 11:09:04 +0200 Subject: keeping the pgp_key accessors for User so views still work --- users/app/models/user.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'users/app/models/user.rb') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index f78f290..0a89f7c 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -87,6 +87,26 @@ class User < CouchRest::Model::Base Ticket.for_user(self).limit(count).all #defaults to having most recent updated first end + # DEPRECATED + # + # Please set the key on the identity directly + # WARNING: This will not be serialized with the user record! + # It is only a workaround for the key form. + def public_key=(value) + identity.set_key(:pgp, value) + end + + # DEPRECATED + # + # Please access identity.keys[:pgp] directly + def public_key + identity.keys[:pgp] + end + + def identity + @identity ||= Identity.for(self) + end + protected ## -- cgit v1.2.3