FEATURE: show "includes ldap aliases" instruction to ldap users

also sends mozilla_iam user custom_fields to users
This commit is contained in:
Leo McArdle 2018-09-11 16:54:23 +01:00
Родитель 51e83d0469
Коммит 6497c39f4e
15 изменённых файлов: 168 добавлений и 62 удалений

Просмотреть файл

@ -6,6 +6,13 @@ export default {
body += "\ninto my account. Thanks!"
component.set("message_admins_body", body)
}
var custom_fields = args.model.mozilla_iam
if (custom_fields) {
if (/^ad|Mozilla-LDAP/.test(custom_fields.uid)) {
component.set("ldap_account", true)
}
}
},
actions: {

Просмотреть файл

@ -37,6 +37,9 @@
<div class="instructions">
{{i18n 'mozilla_iam.user.email.secondary_instructions'}}<br>
{{i18n 'mozilla_iam.user.email.secondary_instructions_further'}}
{{#if ldap_account}}
<br>{{i18n "mozilla_iam.user.email.secondary_instructions_ldap"}}
{{/if}}
</div>
{{#if model.duplicate_accounts}}

Просмотреть файл

@ -1,2 +1 @@
AdminDetailedUserSerializer.include MozillaIAM::AdminDetailedUserSerializerExtensions
AdminDetailedUserSerializer.attributes :mozilla_iam
AdminDetailedUserSerializer.include MozillaIAM::SerializerExtensions::MozillaIAM

Просмотреть файл

@ -5,7 +5,9 @@ class MozillaIAM::DuplicateAccountsUserSerializer < BasicUserSerializer
attributes :email, :secondary_emails
end
UserSerializer.include MozillaIAM::UserSerializerExtensions
UserSerializer.include MozillaIAM::SerializerExtensions::DuplicateAccounts
UserSerializer.has_many :duplicate_accounts,
embed: :object,
serializer: MozillaIAM::DuplicateAccountsUserSerializer
UserSerializer.include MozillaIAM::SerializerExtensions::MozillaIAM

Просмотреть файл

@ -19,6 +19,7 @@ en:
primary_instructions_further: "make the same as your mozillians login identity to link profiles"
secondary_instructions: "can also be used to email discourse"
secondary_instructions_further: "change your mozillians contact identities to edit"
secondary_instructions_ldap: "includes ldap aliases, if you have any"
no_secondary: "No secondary addresses"
duplicate_accounts: "Duplicate Accounts"
duplicate_accounts_instructions: "your duplicate accounts and the emails they've taken"

Просмотреть файл

@ -1,7 +1,7 @@
require_relative 'mozilla_iam/engine'
require_relative 'mozilla_iam/admin_detailed_user_serializer_extensions'
require_relative 'mozilla_iam/user_serializer_extensions'
require_relative 'mozilla_iam/serializer_extensions/duplicate_accounts'
require_relative 'mozilla_iam/serializer_extensions/mozilla_iam'
require_relative 'mozilla_iam/api'
require_relative 'mozilla_iam/api/oauth'
require_relative 'mozilla_iam/api/person'

Просмотреть файл

@ -1,15 +0,0 @@
module MozillaIAM
module AdminDetailedUserSerializerExtensions
def mozilla_iam
object.custom_fields.select do |k, v|
k.start_with?('mozilla_iam')
end.map do |k, v|
key = k.sub('mozilla_iam_', '')
val = Array(v) if Profile.array_keys.include?(key.to_sym)
[key, val || v]
end.to_h
end
end
end

Просмотреть файл

@ -0,0 +1,15 @@
module MozillaIAM
module SerializerExtensions
module DuplicateAccounts
def duplicate_accounts
Array(Profile.for(object)&.duplicate_accounts)
end
def include_duplicate_accounts?
(object&.id == scope.user&.id) || scope.is_admin?
end
end
end
end

Просмотреть файл

@ -0,0 +1,25 @@
module MozillaIAM
module SerializerExtensions
module MozillaIAM
def self.included(c)
c.attributes :mozilla_iam
end
def mozilla_iam
object.custom_fields.select do |k, v|
k.start_with?('mozilla_iam')
end.map do |k, v|
key = k.sub('mozilla_iam_', '')
val = Array(v) if Profile.array_keys.include?(key.to_sym)
[key, val || v]
end.to_h
end
def include_mozilla_iam?
(object&.id == scope.user&.id) || scope.is_staff?
end
end
end
end

Просмотреть файл

@ -1,14 +0,0 @@
module MozillaIAM
module UserSerializerExtensions
def duplicate_accounts
Array(Profile.for(object)&.duplicate_accounts)
end
def include_duplicate_accounts?
(object&.id == scope.user&.id) || scope.is_admin?
end
end
end

Просмотреть файл

@ -16,6 +16,7 @@ end
require 'rails_helper'
require_relative 'support/iam_helpers.rb'
require_relative "support/shared_examples.rb"
require_relative '../db/migrate/20170608165435_create_group_mappings'
CreateGroupMappings.new.migrate(:up) unless ActiveRecord::Base.connection.table_exists? 'mozilla_iam_group_mappings'

Просмотреть файл

@ -6,33 +6,6 @@ describe AdminDetailedUserSerializer do
let(:json) { AdminDetailedUserSerializer.new(user, scope: Guardian.new(admin), root:false).as_json }
describe "#mozilla_iam" do
it "should contain 'mozilla_iam' prefixed custom fields" do
mozilla_iam_one = 'Some IAM data'
mozilla_iam_two = 'Some more IAM data'
user.custom_fields['mozilla_iam_one'] = mozilla_iam_one
user.custom_fields['mozilla_iam_two'] = mozilla_iam_two
user.save
mozilla_iam = json[:mozilla_iam]
expect(mozilla_iam['one']).to eq(mozilla_iam_one)
expect(mozilla_iam['two']).to eq(mozilla_iam_two)
end
it "shouldn't contain non-'mozilla_iam' prefixed custom fields" do
user.custom_fields['other_custom_fields'] = 'some data'
user.save
expect(json[:mozilla_iam]).to be_empty
end
it "should return registered custom fields as arrays" do
MozillaIAM::Profile.stubs(:array_keys).returns([:array])
user.custom_fields['mozilla_iam_array'] = "element"
mozilla_iam = json[:mozilla_iam]
expect(mozilla_iam['array']).to eq ["element"]
end
include_examples "mozilla_iam in serializer"
end
end

Просмотреть файл

@ -147,4 +147,47 @@ describe UserSerializer do
include_examples "not shown"
end
end
describe "#mozilla_iam" do
let(:user) { Fabricate(:user) }
shared_examples "shown" do
include_examples "mozilla_iam in serializer"
end
shared_examples "not shown" do
it "is nil" do
user.custom_fields['mozilla_iam_one'] = "some data"
user.save
mozilla_iam = json[:mozilla_iam]
expect(mozilla_iam).to be_nil
end
end
context "as the user" do
include_context "as the user"
include_examples "shown"
end
context "as an admin" do
include_context "as an admin"
include_examples "shown"
end
context "as a moderator" do
include_context "as a moderator"
include_examples "shown"
end
context "as another user" do
include_context "as another user"
include_examples "not shown"
end
context "as an anonymous user" do
include_context "as an anonymous user"
include_examples "not shown"
end
end
end

Просмотреть файл

@ -0,0 +1,30 @@
shared_examples "mozilla_iam in serializer" do
it "should contain 'mozilla_iam' prefixed custom fields" do
mozilla_iam_one = 'Some IAM data'
mozilla_iam_two = 'Some more IAM data'
user.custom_fields['mozilla_iam_one'] = mozilla_iam_one
user.custom_fields['mozilla_iam_two'] = mozilla_iam_two
user.save
mozilla_iam = json[:mozilla_iam]
expect(mozilla_iam['one']).to eq(mozilla_iam_one)
expect(mozilla_iam['two']).to eq(mozilla_iam_two)
end
it "shouldn't contain non-'mozilla_iam' prefixed custom fields" do
user.custom_fields['other_custom_fields'] = 'some data'
user.save
expect(json[:mozilla_iam]).to be_empty
end
it "should return registered custom fields as arrays" do
MozillaIAM::Profile.stubs(:array_keys).returns([:array])
user.custom_fields['mozilla_iam_array'] = "element"
mozilla_iam = json[:mozilla_iam]
expect(mozilla_iam['array']).to eq ["element"]
end
end

Просмотреть файл

@ -243,3 +243,39 @@ into my account. Thanks!`,
"displays prefilled message in composer"
)
})
QUnit.test("viewing self without ldap account", async assert => {
server.get("/u/eviltrout.json", () => {
return responseWithUserData({
email: "foo",
mozilla_iam: {
uid: "oauth2|firefoxaccounts|lmcardle"
}
})
})
await visit("/u/eviltrout/preferences/account")
assert.notOk(
find(".pref-mozilla-iam-secondary-emails + .instructions").text().includes(" ldap "),
"doesn't show ldap aliases instruction"
)
})
QUnit.test("viewing self with ldap account", async assert => {
server.get("/u/eviltrout.json", () => {
return responseWithUserData({
email: "foo",
mozilla_iam: {
uid: "ad|Mozilla-LDAP|lmcardle"
}
})
})
await visit("/u/eviltrout/preferences/account")
assert.ok(
find(".pref-mozilla-iam-secondary-emails + .instructions").text().includes(" ldap "),
"shows ldap aliases instruction"
)
})