summaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2013-07-19 11:09:25 +0200
committerAzul <azul@leap.se>2013-07-24 10:55:51 +0200
commit8ddbaa6184e4dbcc6ef7e81cf555cc18d3822dce (patch)
tree2fd33652e8cf96b645d58a83822258c5673f2045 /users
parentb6242bbefc1e9fe193bbf3479e8fa822829c6d1a (diff)
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.
Diffstat (limited to 'users')
-rw-r--r--users/app/models/user.rb44
-rw-r--r--users/test/unit/identity_test.rb6
-rw-r--r--users/test/unit/user_test.rb14
3 files changed, 48 insertions, 16 deletions
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)
diff --git a/users/test/unit/identity_test.rb b/users/test/unit/identity_test.rb
index 6b0a6b1..d20ad93 100644
--- a/users/test/unit/identity_test.rb
+++ b/users/test/unit/identity_test.rb
@@ -11,7 +11,7 @@ class IdentityTest < ActiveSupport::TestCase
end
test "initial identity for a user" do
- id = @user.build_identity
+ id = @user.identity
assert_equal @user.email_address, id.address
assert_equal @user.email_address, id.destination
assert_equal @user, id.user
@@ -56,13 +56,13 @@ class IdentityTest < ActiveSupport::TestCase
end
test "setting and getting pgp key" do
- id = @user.build_identity
+ id = @user.identity
id.keys[:pgp] = pgp_key_string
assert_equal pgp_key_string, id.keys[:pgp]
end
test "querying pgp key via couch" do
- id = @user.build_identity
+ id = @user.identity
id.keys[:pgp] = pgp_key_string
id.save
view = Identity.pgp_key_by_email.key(id.address)
diff --git a/users/test/unit/user_test.rb b/users/test/unit/user_test.rb
index c8c837b..5d1fe06 100644
--- a/users/test/unit/user_test.rb
+++ b/users/test/unit/user_test.rb
@@ -56,23 +56,21 @@ class UserTest < ActiveSupport::TestCase
other_user.destroy
end
- test "login needs to be different from other peoples email aliases" do
- other_user = FactoryGirl.create :user
- other_user.email_aliases.build :email => @user.login
- other_user.save
- assert !@user.valid?
- other_user.destroy
+ test "deprecated public key api still works" do
+ key = SecureRandom.base64(4096)
+ @user.public_key = key
+ assert_equal key, @user.public_key
end
test "pgp key view" do
@user.public_key = SecureRandom.base64(4096)
@user.save
- view = User.pgp_key_by_handle.key(@user.login)
+ view = Identity.pgp_key_by_email.key(@user.email_address)
assert_equal 1, view.rows.count
assert result = view.rows.first
- assert_equal @user.login, result["key"]
+ assert_equal @user.email_address, result["key"]
assert_equal @user.public_key, result["value"]
end
end