From 296cdd591f7f01ffdbe18cd6a839bbd0e624dfba Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 6 Nov 2012 14:30:48 +0100 Subject: [PATCH] Add optional signup. --- app/assets/stylesheets/sections/login.scss | 5 ++++ app/controllers/registrations_controller.rb | 9 ++++++ app/models/user.rb | 2 +- app/views/devise/passwords/new.html.erb | 2 +- app/views/devise/registrations/new.html.erb | 18 ------------ app/views/devise/registrations/new.html.haml | 19 +++++++++++++ app/views/devise/sessions/new.html.haml | 8 +++++- app/views/notify/new_user_email.html.haml | 10 +++++-- config/gitlab.yml.example | 1 + config/initializers/1_settings.rb | 1 + config/routes.rb | 2 +- spec/mailers/notify_spec.rb | 30 ++++++++++++++++++++ spec/requests/admin/admin_users_spec.rb | 13 +++++++++ spec/requests/api/users_spec.rb | 30 ++++++++++++++++++++ 14 files changed, 125 insertions(+), 25 deletions(-) create mode 100644 app/controllers/registrations_controller.rb delete mode 100644 app/views/devise/registrations/new.html.erb create mode 100644 app/views/devise/registrations/new.html.haml diff --git a/app/assets/stylesheets/sections/login.scss b/app/assets/stylesheets/sections/login.scss index 8c21de70..7536abff 100644 --- a/app/assets/stylesheets/sections/login.scss +++ b/app/assets/stylesheets/sections/login.scss @@ -31,4 +31,9 @@ body.login-page{ margin-bottom: 20px; } +.login-box input.text.middle{ + border-top: 0; + margin-bottom:0px; +} + .login-box a.forgot{float: right; padding-top: 6px} diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 00000000..cbac7613 --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,9 @@ +class RegistrationsController < Devise::RegistrationsController + before_filter :signup_enabled? + + private + + def signup_enabled? + redirect_to new_user_session_path unless Gitlab.config.gitlab.signup_enabled + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 55d75892..7e69f3db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,7 +35,7 @@ class User < ActiveRecord::Base devise :database_authenticatable, :token_authenticatable, :lockable, - :recoverable, :rememberable, :trackable, :validatable, :omniauthable + :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :registerable attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, :name, :username, :skype, :linkedin, :twitter, :dark_scheme, :theme_id, :force_random_password, diff --git a/app/views/devise/passwords/new.html.erb b/app/views/devise/passwords/new.html.erb index cf56c5d2..860d67d1 100644 --- a/app/views/devise/passwords/new.html.erb +++ b/app/views/devise/passwords/new.html.erb @@ -5,5 +5,5 @@

<%= f.submit "Reset password", :class => "primary btn" %> -
<%= render :partial => "devise/shared/links" %>
+
<%= link_to "Sign in", new_session_path(resource_name), :class => "btn" %>
<% end %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb deleted file mode 100644 index 4ac617c5..00000000 --- a/app/views/devise/registrations/new.html.erb +++ /dev/null @@ -1,18 +0,0 @@ -

Sign up

- -<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> - <%= devise_error_messages! %> - -
<%= f.label :email %>
- <%= f.email_field :email %>
- -
<%= f.label :password %>
- <%= f.password_field :password %>
- -
<%= f.label :password_confirmation %>
- <%= f.password_field :password_confirmation %>
- -
<%= f.submit "Sign up", :class => "input_button" %>
-<% end %> - -<%= render :partial => "devise/shared/links" %> diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml new file mode 100644 index 00000000..81eb2622 --- /dev/null +++ b/app/views/devise/registrations/new.html.haml @@ -0,0 +1,19 @@ += form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => "login-box" }) do |f| + = image_tag "login-logo.png", :width => "304", :height => "66", :class => "login-logo", :alt => "Login Logo" + = devise_error_messages! + %div + = f.text_field :name, :class => "text top", :placeholder => "Name", :required => true + %div + = f.text_field :username, :class => "text middle", :placeholder => "Username", :required => true + %div + = f.email_field :email, :class => "text middle", :placeholder => "Email", :required => true + %div + = f.password_field :password, :class => "text middle", :placeholder => "Password", :required => true + %div + = f.password_field :password_confirmation, :class => "text bottom", :placeholder => "Confirm password", :required => true + %div + = f.submit "Sign up", :class => "primary btn wide" + %br + %hr + = link_to "Sign in", new_session_path(resource_name) + = link_to "Forgot your password?", new_password_path(resource_name), :class => "right" diff --git a/app/views/devise/sessions/new.html.haml b/app/views/devise/sessions/new.html.haml index 474e7ef7..0983f315 100644 --- a/app/views/devise/sessions/new.html.haml +++ b/app/views/devise/sessions/new.html.haml @@ -13,7 +13,13 @@ %br/ = f.submit "Sign in", :class => "primary btn wide" .right - = render :partial => "devise/shared/links" + = link_to "Forgot your password?", new_password_path(resource_name), :class => "btn" + %br/ + %br/ + - if Gitlab.config.gitlab.signup_enabled + %hr/ + Don't have an account? + = link_to "Sign up", new_registration_path(resource_name) .clearfix - if devise_mapping.omniauthable? && resource_class.omniauth_providers.present? %div diff --git a/app/views/notify/new_user_email.html.haml b/app/views/notify/new_user_email.html.haml index 93bf7c50..e8e97355 100644 --- a/app/views/notify/new_user_email.html.haml +++ b/app/views/notify/new_user_email.html.haml @@ -6,7 +6,10 @@ %h2{style: "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} Hi #{@user['name']}! %p{style: "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 20px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} - Administrator created account for you. Now you are a member of company GitLab application. + - if Gitlab.config.gitlab.signup_enabled + Account has been created successfully. + - else + Administrator created account for you. Now you are a member of company GitLab application. %td{style: "font-size: 1px; line-height: 1px;", width: "21"} %tr %td{style: "font-size: 1px; line-height: 1px;", width: "21"} @@ -15,8 +18,9 @@ login.......................................... %code= @user['email'] %p{style: "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 28px; font-size: 16px;font-family: Helvetica, Arial, sans-serif; "} - password.................................. - %code= @password + - unless Gitlab.config.gitlab.signup_enabled + password.................................. + %code= @password %p{style: "color:#767676; font-weight: normal; margin: 0; padding: 0; line-height: 28px; font-size: 12px;font-family: Helvetica, Arial, sans-serif; "} = link_to "Click here to login", root_url %td{style: "font-size: 1px; line-height: 1px;", width: "21"} diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index c1266daf..26bd0696 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -31,6 +31,7 @@ gitlab: ## Project settings default_projects_limit: 10 + # signup_enabled: true # default: false - Account passwords are not sent via the email if signup is enabled. ## Gravatar gravatar: diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index 87c2399e..4e31b65f 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -51,6 +51,7 @@ Settings.gitlab['protocol'] ||= Settings.gitlab.https ? "https" : "http" Settings.gitlab['email_from'] ||= "gitlab@#{Settings.gitlab.host}" Settings.gitlab['url'] ||= Settings.send(:build_gitlab_url) Settings.gitlab['user'] ||= 'gitlab' +Settings.gitlab['signup_enabled'] ||= false Settings['gravatar'] ||= Settingslogic.new({}) Settings.gravatar['enabled'] = true if Settings.gravatar['enabled'].nil? diff --git a/config/routes.rb b/config/routes.rb index ee6dfe6d..44681735 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -121,7 +121,7 @@ Gitlab::Application.routes.draw do resources :projects, constraints: { id: /[^\/]+/ }, only: [:new, :create] - devise_for :users, controllers: { omniauth_callbacks: :omniauth_callbacks } + devise_for :users, controllers: { omniauth_callbacks: :omniauth_callbacks, registrations: :registrations } # # Project Area diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb index 01e3c3f1..d947f0e2 100644 --- a/spec/mailers/notify_spec.rb +++ b/spec/mailers/notify_spec.rb @@ -32,6 +32,7 @@ describe Notify do end it 'contains the new user\'s password' do + Gitlab.config.gitlab.stub(:signup_enabled).and_return(false) should have_body_text /#{new_user.password}/ end @@ -40,6 +41,35 @@ describe Notify do end end + + describe 'for users that signed up, the email' do + let(:example_site_path) { root_path } + let(:new_user) { create(:user, email: 'newguy@example.com', password: "securePassword") } + + subject { Notify.new_user_email(new_user.id, new_user.password) } + + it 'is sent to the new user' do + should deliver_to new_user.email + end + + it 'has the correct subject' do + should have_subject /^gitlab \| Account was created for you$/i + end + + it 'contains the new user\'s login name' do + should have_body_text /#{new_user.email}/ + end + + it 'should not contain the new user\'s password' do + Gitlab.config.gitlab.stub(:signup_enabled).and_return(true) + should_not have_body_text /#{new_user.password}/ + end + + it 'includes a link to the site' do + should have_body_text /#{example_site_path}/ + end + end + context 'for a project' do describe 'items that are assignable, the email' do let(:assignee) { create(:user, email: 'assignee@example.com') } diff --git a/spec/requests/admin/admin_users_spec.rb b/spec/requests/admin/admin_users_spec.rb index a66e85a3..455caf4a 100644 --- a/spec/requests/admin/admin_users_spec.rb +++ b/spec/requests/admin/admin_users_spec.rb @@ -49,6 +49,7 @@ describe "Admin::Users" do end it "should send valid email to user with email & password" do + Gitlab.config.gitlab.stub(:signup_enabled).and_return(false) User.observers.enable :user_observer do click_button "Save" user = User.last @@ -58,6 +59,18 @@ describe "Admin::Users" do email.body.should have_content(@password) end end + + it "should send valid email to user with email without password when signup is enabled" do + Gitlab.config.gitlab.stub(:signup_enabled).and_return(true) + User.observers.enable :user_observer do + click_button "Save" + user = User.last + email = ActionMailer::Base.deliveries.last + email.subject.should have_content("Account was created") + email.body.should have_content(user.email) + email.body.should_not have_content(@password) + end + end end describe "GET /admin/users/:id" do diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 4cfb4884..ee5f510a 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -53,6 +53,36 @@ describe Gitlab::API do end end + describe "GET /users/sign_up" do + before do + Gitlab.config.gitlab.stub(:signup_enabled).and_return(false) + end + it "should redirect to sign in page if signup is disabled" do + get "/users/sign_up" + response.status.should == 302 + response.should redirect_to(new_user_session_path) + end + end + + describe "GET /users/sign_up" do + before do + Gitlab.config.gitlab.stub(:signup_enabled).and_return(true) + end + it "should return sign up page if signup is enabled" do + get "/users/sign_up" + response.status.should == 200 + end + it "should create a new user account" do + visit new_user_registration_path + fill_in "user_name", with: "Name Surname" + fill_in "user_username", with: "Great" + fill_in "user_email", with: "name@mail.com" + fill_in "user_password", with: "password1234" + fill_in "user_password_confirmation", with: "password1234" + expect { click_button "Sign up" }.to change {User.count}.by(1) + end + end + describe "GET /user" do it "should return current user" do get api("/user", user)