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/account_settings.rb | 36 +++++++++++++++++++++++ users/app/models/identity.rb | 27 +++++++++++++++++ users/app/models/signup_service.rb | 9 ++++++ users/app/models/user.rb | 57 ------------------------------------ 4 files changed, 72 insertions(+), 57 deletions(-) create mode 100644 users/app/models/account_settings.rb create mode 100644 users/app/models/signup_service.rb (limited to 'users/app/models') diff --git a/users/app/models/account_settings.rb b/users/app/models/account_settings.rb new file mode 100644 index 0000000..a73a95a --- /dev/null +++ b/users/app/models/account_settings.rb @@ -0,0 +1,36 @@ +class AccountSettings + + def initialize(user) + @user = user + end + + def update(attrs) + if attrs[:password_verifier].present? + update_login(attrs[:login]) + @user.update_attributes attrs.slice(:password_verifier, :password_salt) + end + # TODO: move into identity controller + update_pgp_key(attrs[:public_key]) if attrs.has_key? :public_key + @user.save && save_identities + end + + protected + + def update_login(login, verifier) + return unless login.present? + @old_identity = Identity.for(@user) + @user.login = login + @new_identity = Identity.for(@user) # based on the new login + @old_identity.destination = @user.email_address # alias old -> new + end + + def update_pgp_key(key) + @new_identity ||= Identity.for(@user) + @new_identity.set_key(:pgp, key) + end + + def save_identities + @new_identity.try(:save) && @old_identity.try(:save) + end + +end diff --git a/users/app/models/identity.rb b/users/app/models/identity.rb index 73531ec..355f67a 100644 --- a/users/app/models/identity.rb +++ b/users/app/models/identity.rb @@ -27,6 +27,33 @@ class Identity < CouchRest::Model::Base end + def self.for(user, attributes = {}) + find_for(user, attributes) || build_for(user, attributes) + end + + def self.find_for(user, attributes = {}) + attributes.reverse_merge! attributes_from_user(user) + find_by_address_and_destination [attributes[:address], attributes[:destination]] + end + + def self.build_for(user, attributes = {}) + attributes.reverse_merge! attributes_from_user(user) + Identity.new(attributes) + end + + def self.create_for(user, attributes = {}) + identity = build_for(user, attributes) + identity.save + identity + end + + def self.attributes_from_user(user) + { user_id: user.id, + address: user.email_address, + destination: user.email_address + } + end + def keys read_attribute('keys') || HashWithIndifferentAccess.new end diff --git a/users/app/models/signup_service.rb b/users/app/models/signup_service.rb new file mode 100644 index 0000000..f316ca9 --- /dev/null +++ b/users/app/models/signup_service.rb @@ -0,0 +1,9 @@ +class SignupService + + def register(attrs) + User.create(attrs).tap do |user| + Identity.create_for user + end + end + +end 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