From b6aaad87af41ca24d52849db71b28500609a6504 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 7 Oct 2015 20:18:17 +0200 Subject: [PATCH] Adds a user controller for management --- app/models/user.rb | 7 +++ .../document_verifications/index.html.erb | 2 +- .../email_verifications/new.html.erb | 2 +- app/views/management/users/new.html.erb | 25 +++++++++++ app/views/management/users/show.html.erb | 9 ++++ config/locales/management.en.yml | 8 ++++ config/routes.rb | 2 + spec/features/management/users_spec.rb | 45 +++++++++++++++++++ 8 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 app/views/management/users/new.html.erb create mode 100644 app/views/management/users/show.html.erb create mode 100644 spec/features/management/users_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index 80e2de195..f5a05e3ba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,6 +35,8 @@ class User < ActiveRecord::Base accepts_nested_attributes_for :organization, update_only: true + attr_accessor :skip_password_validation + scope :administrators, -> { joins(:administrators) } scope :moderators, -> { joins(:moderator) } scope :organizations, -> { joins(:organization) } @@ -162,6 +164,11 @@ class User < ActiveRecord::Base sign_in_count == 1 && unverified? && !organization && !administrator? end + def password_required? + return false if skip_password_validation + super + end + private def validate_username_length diff --git a/app/views/management/document_verifications/index.html.erb b/app/views/management/document_verifications/index.html.erb index d87b83b06..20d7724b4 100644 --- a/app/views/management/document_verifications/index.html.erb +++ b/app/views/management/document_verifications/index.html.erb @@ -1,4 +1,4 @@ -

<%= t("management.users.title") %>

+

<%= t("management.document_verifications.title") %>

diff --git a/app/views/management/email_verifications/new.html.erb b/app/views/management/email_verifications/new.html.erb index 52e20a525..b8094a2b1 100644 --- a/app/views/management/email_verifications/new.html.erb +++ b/app/views/management/email_verifications/new.html.erb @@ -25,7 +25,7 @@ <%= t("management.email_verifications.if_no_existing_account") %>

- <%= t("management.print_info") %> + <%= link_to t('management.users.create_user'), new_management_user_path(user: params[:email_verification]), class: "button warning radius" %>

diff --git a/app/views/management/users/new.html.erb b/app/views/management/users/new.html.erb new file mode 100644 index 000000000..1188b7872 --- /dev/null +++ b/app/views/management/users/new.html.erb @@ -0,0 +1,25 @@ +<%= render 'management/account_info.html', account: @user %> + +

<%= t("management.users.create_user_info") %>

+ +<%= render 'management/user_permissions', + message: t("management.document_verifications.in_census_has_following_permissions"), + permissions: [:debates, :create_proposals, :support_proposals, :vote_proposals] %> + +
+
+ <%= form_for @user, url: management_users_path do |f| %> + <%= f.hidden_field :document_type %> + <%= f.hidden_field :document_number %> + <%= f.text_field :username, + label: t('management.username_label'), + placeholder: t('management.username_label') %> + <%= f.text_field :email, + label: t('management.email_label'), + placeholder: t('management.email_label') %> + + <%= f.submit t("management.users.create_user_submit"), class: "button success radius" %> + <% end %> +
+
+ diff --git a/app/views/management/users/show.html.erb b/app/views/management/users/show.html.erb new file mode 100644 index 000000000..ce4257bea --- /dev/null +++ b/app/views/management/users/show.html.erb @@ -0,0 +1,9 @@ +<%= render 'management/account_info.html', account: @user %> + +

<%= t("management.users.create_user_success_html", + link: link_to("http://decide.madrid.es", "http://decide.madrid.es", target: "_blank")) %> +

+ +<%= render 'management/user_permissions', + message: t("management.document_verifications.in_census_has_following_permissions"), + permissions: [:debates, :create_proposals, :support_proposals, :vote_proposals] %> diff --git a/config/locales/management.en.yml b/config/locales/management.en.yml index d0084264b..1d7f64a30 100644 --- a/config/locales/management.en.yml +++ b/config/locales/management.en.yml @@ -36,3 +36,11 @@ en: if_no_existing_account: "If this person has not created an account yet" document_mismatch: "This email belongs to a user which already has an associated id: %{document_number}(%{document_type})" already_verified: "This user account is already verified." + users: + create_user: "Create a new account" + create_user_info: "We will create an account with the following data:" + create_user_submit: "Create user" + create_user_success_html: + "We have sent an email to the email address used to create the account in order to verify that it belongs to him. + We recommend changing the password on the first login. In order to do that go to %{link} with your user and password, + and enter the 'My account / Change access data' section" diff --git a/config/routes.rb b/config/routes.rb index 50d8283d2..c4828414b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -173,6 +173,8 @@ Rails.application.routes.draw do end resources :email_verifications, only: [:new, :create] + + resources :users, only: [:new, :create] end # Example of regular route: diff --git a/spec/features/management/users_spec.rb b/spec/features/management/users_spec.rb new file mode 100644 index 000000000..9b648f6ee --- /dev/null +++ b/spec/features/management/users_spec.rb @@ -0,0 +1,45 @@ +require 'rails_helper' + +feature 'users' do + + scenario 'Creating a level 3 user from scratch' do + + visit management_document_verifications_path + fill_in 'document_verification_document_number', with: '1234' + click_button 'Check' + + expect(page).to have_content "Please introduce the email used on the account" + + click_link 'Create a new account' + + fill_in 'user_username', with: 'pepe' + fill_in 'user_email', with: 'pepe@gmail.com' + + click_button 'Create user' + + expect(page).to have_content "We have sent an email" + + user = User.find_by_email('pepe@gmail.com') + + expect(user).to be_level_three_verified + expect(user).to be_residence_verified + expect(user).to_not be_confirmed + + sent_token = /.*confirmation_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1] + visit user_confirmation_path(confirmation_token: sent_token) + + expect(page).to have_content "Confirming the account with email" + + fill_in 'user_password', with: '12345678' + fill_in 'user_password_confirmation', with: '12345678' + + click_button 'Confirm' + + expect(user.reload).to be_confirmed + + expect(page).to have_content "Your email address has been successfully confirmed." + end +end + + +