summaryrefslogtreecommitdiff
path: root/users/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'users/app/models')
-rw-r--r--users/app/models/account_settings.rb36
-rw-r--r--users/app/models/identity.rb27
-rw-r--r--users/app/models/signup_service.rb9
-rw-r--r--users/app/models/user.rb57
4 files changed, 72 insertions, 57 deletions
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
##