aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/user.rb11
-rw-r--r--config/locales/users.en.yml8
-rw-r--r--config/locales/users.fr.yml10
-rw-r--r--db/migrate/20170113155639_add_permissions_to_users.rb5
-rw-r--r--db/schema.rb21
-rw-r--r--spec/models/user_spec.rb19
6 files changed, 52 insertions, 22 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 5cfdf0605..fe5d9910d 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -21,6 +21,7 @@ class User < ActiveRecord::Base
validates :organisation, :presence => true
validates :email, :presence => true, :uniqueness => true
validates :name, :presence => true
+ validate :permissions_unique_and_nonempty
before_validation(:on => :create) do
self.password ||= Devise.friendly_token.first(6)
@@ -73,4 +74,14 @@ class User < ActiveRecord::Base
organisation.destroy
end
end
+
+ def permissions_unique_and_nonempty
+ if permissions && permissions.any?
+ if permissions.uniq.length != permissions.length
+ errors.add(:permissions, I18n.t('activerecord.errors.models.calendar.attributes.permissions.must_be_unique'))
+ elsif permissions.include? ''
+ errors.add(:permissions, I18n.t('activerecord.errors.models.calendar.attributes.permissions.must_be_nonempty'))
+ end
+ end
+ end
end
diff --git a/config/locales/users.en.yml b/config/locales/users.en.yml
index 19168f560..37d96e7a3 100644
--- a/config/locales/users.en.yml
+++ b/config/locales/users.en.yml
@@ -20,3 +20,11 @@ en:
user:
name: "Full name"
username: "Username"
+ permissions: Permissions
+ errors:
+ models:
+ calendar:
+ attributes:
+ permissions:
+ must_be_unique: User's permissions must be unique.
+ must_be_nonempty: A permission can't be empty.
diff --git a/config/locales/users.fr.yml b/config/locales/users.fr.yml
index f50218605..b0329e77e 100644
--- a/config/locales/users.fr.yml
+++ b/config/locales/users.fr.yml
@@ -20,5 +20,11 @@ fr:
user:
name: "Nom complet"
username: "Nom d'utilisateur"
-
-
+ permissions: Permissions
+ errors:
+ models:
+ calendar:
+ attributes:
+ permissions:
+ must_be_unique: Une permission ne peut pas apparaître deux fois chez un même utilisateur.
+ must_be_nonempty: Une permission ne peut pas être une chaine vide.
diff --git a/db/migrate/20170113155639_add_permissions_to_users.rb b/db/migrate/20170113155639_add_permissions_to_users.rb
new file mode 100644
index 000000000..abfe14ccd
--- /dev/null
+++ b/db/migrate/20170113155639_add_permissions_to_users.rb
@@ -0,0 +1,5 @@
+class AddPermissionsToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :permissions, :string, array: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index c9ef05ab1..fe0ddec32 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170106135000) do
+ActiveRecord::Schema.define(version: 20170113155639) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -161,22 +161,6 @@ ActiveRecord::Schema.define(version: 20170106135000) do
add_index "connection_links", ["objectid"], :name => "connection_links_objectid_key", :unique => true
- create_table "delayed_jobs", force: true do |t|
- t.integer "priority", default: 0
- t.integer "attempts", default: 0
- t.text "handler"
- t.text "last_error"
- t.datetime "run_at"
- t.datetime "locked_at"
- t.datetime "failed_at"
- t.string "locked_by"
- t.string "queue"
- t.datetime "created_at"
- t.datetime "updated_at"
- end
-
- add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
-
create_table "exports", force: true do |t|
t.integer "referential_id", limit: 8
t.string "status"
@@ -718,6 +702,7 @@ ActiveRecord::Schema.define(version: 20170106135000) do
t.datetime "invitation_created_at"
t.string "username"
t.datetime "synced_at"
+ t.string "permissions", array: true
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
@@ -777,8 +762,6 @@ ActiveRecord::Schema.define(version: 20170106135000) do
add_index "workbenches", ["stop_area_referential_id"], :name => "index_workbenches_on_stop_area_referential_id"
Foreigner.load
- add_foreign_key "access_links", "access_points", name: "aclk_acpt_fkey", dependent: :delete
-
add_foreign_key "group_of_lines_lines", "group_of_lines", name: "groupofline_group_fkey", dependent: :delete
add_foreign_key "journey_frequencies", "timebands", name: "journey_frequencies_timeband_id_fk", dependent: :nullify
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 7d0a548c1..bbeb0caf5 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -13,7 +13,8 @@ describe User, :type => :model do
:email => 'john.doe@af83.com',
:organisation_code => '0083',
:organisation_name => 'af83',
- :functional_scope => "[\"STIF:CODIFLIGNE:Line:C00840\", \"STIF:CODIFLIGNE:Line:C00086\"]"
+ :functional_scope => "[\"STIF:CODIFLIGNE:Line:C00840\", \"STIF:CODIFLIGNE:Line:C00086\"]",
+ :permissions => nil
}
ticket.user = "john.doe"
ticket.success = true
@@ -115,6 +116,22 @@ describe User, :type => :model do
end
end
+ describe 'validations' do
+ it 'validates uniqueness of pemissions' do
+ user = build :user, permissions: Array.new(2, 'calendars.shared')
+ expect {
+ user.save!
+ }.to raise_error(ActiveRecord::RecordInvalid)
+ end
+
+ it 'validates no pemission is an empty string' do
+ user = build :user, permissions: ['']
+ expect {
+ user.save!
+ }.to raise_error(ActiveRecord::RecordInvalid)
+ end
+ end
+
describe "#destroy" do
let!(:organisation){create(:organisation)}
let!(:user){create(:user, :organisation => organisation)}