Skip to content

Commit 6838304

Browse files
committed
Force user password change for users created by admin
1 parent 00882b3 commit 6838304

File tree

9 files changed

+118
-76
lines changed

9 files changed

+118
-76
lines changed

app/controllers/admin/users_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ def unblock
5555
def create
5656
admin = params[:user].delete("admin")
5757

58-
@admin_user = User.new(params[:user], as: :admin)
58+
opts = {
59+
force_random_password: true,
60+
password_expires_at: Time.now
61+
}
62+
63+
@admin_user = User.new(params[:user].merge(opts), as: :admin)
5964
@admin_user.admin = (admin && admin.to_i > 0)
65+
@admin_user.created_by_id = current_user.id
6066

6167
respond_to do |format|
6268
if @admin_user.save

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,8 @@ def ldap_user?
367367
def accessible_deploy_keys
368368
DeployKey.in_projects(self.master_projects).uniq
369369
end
370+
371+
def created_by
372+
User.find_by_id(created_by_id) if created_by_id
373+
end
370374
end

app/views/admin/users/_form.html.haml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,25 @@
2424
= f.text_field :email, required: true, autocomplete: "off"
2525
%span.help-inline * required
2626

27-
%fieldset
28-
%legend Password
29-
.clearfix
30-
= f.label :password
31-
.input= f.password_field :password, disabled: f.object.force_random_password
32-
.clearfix
33-
= f.label :password_confirmation
34-
.input= f.password_field :password_confirmation, disabled: f.object.force_random_password
35-
-if f.object.new_record?
27+
- if @admin_user.new_record?
28+
%fieldset
29+
%legend Password
30+
.clearfix
31+
= f.label :password
32+
.input
33+
%strong
34+
A temporary password will be generated and sent to user.
35+
%br
36+
User will be forced to change it after first sign in
37+
- else
38+
%fieldset
39+
%legend Password
40+
.clearfix
41+
= f.label :password
42+
.input= f.password_field :password, disabled: f.object.force_random_password
3643
.clearfix
37-
= f.label :force_random_password do
38-
%span Generate random password
39-
.input= f.check_box :force_random_password, {}, true, nil
44+
= f.label :password_confirmation
45+
.input= f.password_field :password_confirmation, disabled: f.object.force_random_password
4046

4147
%fieldset
4248
%legend Access

app/views/admin/users/show.html.haml

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,65 @@
1+
%h3.page_title
2+
User:
3+
= @admin_user.name
4+
- if @admin_user.blocked?
5+
%span.cred (Blocked)
6+
- if @admin_user.admin
7+
%span.cred (Admin)
8+
9+
.pull-right
10+
= link_to edit_admin_user_path(@admin_user), class: "btn grouped btn-small" do
11+
%i.icon-edit
12+
Edit
13+
- unless @admin_user == current_user
14+
- if @admin_user.blocked?
15+
= link_to 'Unblock', unblock_admin_user_path(@admin_user), method: :put, class: "btn grouped btn-small success"
16+
- else
17+
= link_to 'Block', block_admin_user_path(@admin_user), confirm: 'USER WILL BE BLOCKED! Are you sure?', method: :put, class: "btn grouped btn-small btn-remove"
18+
= link_to 'Destroy', [:admin, @admin_user], confirm: "USER #{@admin_user.name} WILL BE REMOVED! Are you sure?", method: :delete, class: "btn grouped btn-small btn-remove"
19+
%hr
20+
121
.row
222
.span6
3-
%h3.page_title
4-
= image_tag gravatar_icon(@admin_user.email, 90), class: "avatar s90"
5-
= @admin_user.name
6-
- if @admin_user.blocked?
7-
%span.cred (Blocked)
8-
- if @admin_user.admin
9-
%span.cred (Admin)
10-
.pull-right
11-
= link_to edit_admin_user_path(@admin_user), class: "btn pull-right" do
12-
%i.icon-edit
13-
Edit
14-
%br
15-
%small @#{@admin_user.username}
16-
%br
17-
%small member since #{@admin_user.created_at.stamp("Nov 12, 2031")}
18-
.clearfix
19-
%hr
20-
%p
21-
%span.btn.btn-small
22-
%i.icon-envelope
23-
= mail_to @admin_user.email
24-
- unless @admin_user == current_user
25-
- if @admin_user.blocked?
26-
= link_to 'Unblock', unblock_admin_user_path(@admin_user), method: :put, class: "btn btn-small success"
27-
- else
28-
= link_to 'Block', block_admin_user_path(@admin_user), confirm: 'USER WILL BE BLOCKED! Are you sure?', method: :put, class: "btn btn-small btn-remove"
29-
= link_to 'Destroy', [:admin, @admin_user], confirm: "USER #{@admin_user.name} WILL BE REMOVED! Are you sure?", method: :delete, class: "btn btn-small btn-remove"
23+
.ui-box
24+
%h5.title
25+
Account:
26+
.pull-right
27+
= image_tag gravatar_icon(@admin_user.email, 32), class: "avatar s32"
28+
%ul.well-list
29+
%li
30+
%span.light Name:
31+
%strong= @admin_user.name
32+
%li
33+
%span.light Username:
34+
%strong
35+
= @admin_user.username
36+
%li
37+
%span.light Email:
38+
%strong
39+
= mail_to @admin_user.email
40+
41+
%li
42+
%span.light Member since:
43+
%strong
44+
= @admin_user.created_at.stamp("Nov 12, 2031")
45+
46+
%li
47+
%span.light Last sign-in at:
48+
%strong
49+
= @admin_user.last_sign_in_at.stamp("Nov 12, 2031")
50+
51+
- if @admin_user.ldap_user?
52+
%li
53+
%span.light LDAP uid:
54+
%strong
55+
= @admin_user.extern_uid
56+
57+
- if @admin_user.created_by
58+
%li
59+
%span.light Created by:
60+
%strong
61+
= link_to @admin_user.created_by.name, [:admin, @admin_user.created_by]
62+
3063
%hr
3164
%h5
3265
Add User to Projects
@@ -67,28 +100,29 @@
67100

68101

69102
.span6
70-
= render 'users/profile', user: @admin_user
71103
.ui-box
72104
%h5.title Projects (#{@projects.count})
73105
%ul.well-list
74106
- @projects.sort_by(&:name_with_namespace).each do |project|
107+
- tm = project.team.get_tm(@admin_user.id)
75108
%li
76109
= link_to admin_project_path(project), class: dom_class(project) do
77110
- if project.namespace
78111
= project.namespace.human_name
79112
\/
80113
%strong.well-title
81114
= truncate(project.name, length: 45)
82-
%span.pull-right.light
83-
- if project.owner == @admin_user
84-
%i.icon-wrench
85-
- tm = project.team.get_tm(@admin_user.id)
86-
- if tm
87-
= tm.project_access_human
88-
= link_to edit_admin_project_member_path(project, tm.user), class: "btn btn-small" do
115+
116+
- if project.owner == @admin_user
117+
%span.label.label-info owner
118+
119+
- if tm
120+
.pull-right
121+
= link_to edit_admin_project_member_path(project, tm.user), class: "btn grouped btn-small" do
89122
%i.icon-edit
90-
= link_to admin_project_member_path(project, tm.user), confirm: remove_from_project_team_message(project, @admin_user), method: :delete, class: "btn btn-small btn-remove" do
123+
= link_to admin_project_member_path(project, tm.user), confirm: remove_from_project_team_message(project, @admin_user), method: :delete, class: "btn grouped btn-small btn-remove" do
91124
%i.icon-remove
92-
%p.light
93-
%i.icon-wrench
94-
– user is a project owner
125+
126+
.pull-right.light
127+
= tm.project_access_human
128+
 

app/views/notify/new_user_email.html.haml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
%p
99
login..........................................
1010
%code= @user['email']
11-
%p
12-
- unless Gitlab.config.gitlab.signup_enabled
11+
12+
- if @user.created_by_id
13+
%p
1314
password..................................
1415
%code= @password
1516

16-
%p
17-
Please change your password immediately after login.
17+
%p
18+
You will be forced to change this password immediately after login.
1819

1920
%p
2021
= link_to "Click here to login", root_url

app/views/notify/new_user_email.text.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ Hi <%= @user.name %>!
33
The Administrator created an account for you. Now you are a member of company GitLab application.
44

55
login.................. <%= @user.email %>
6-
<% unless Gitlab.config.gitlab.signup_enabled %>
6+
<% if @user.created_by_id %>
77
password............... <%= @password %>
8+
9+
You will be forced to change this password immediately after login.
810
<% end %>
911

10-
Please change your password immediately after login.
1112

1213
Click here to login: <%= url_for(root_url) %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddCreatedByIdToUser < ActiveRecord::Migration
2+
def change
3+
add_column :users, :created_by_id, :integer
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20130613165816) do
14+
ActiveRecord::Schema.define(:version => 20130613173246) do
1515

1616
create_table "deploy_keys_projects", :force => true do |t|
1717
t.integer "deploy_key_id", :null => false
@@ -293,6 +293,7 @@
293293
t.integer "color_scheme_id", :default => 1, :null => false
294294
t.integer "notification_level", :default => 1, :null => false
295295
t.datetime "password_expires_at"
296+
t.integer "created_by_id"
296297
end
297298

298299
add_index "users", ["admin"], :name => "index_users_on_admin"

spec/features/admin/admin_users_spec.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020

2121
describe "GET /admin/users/new" do
2222
before do
23-
@password = "123ABC"
2423
visit new_admin_user_path
2524
fill_in "user_name", with: "Big Bang"
2625
fill_in "user_username", with: "bang"
2726
fill_in "user_email", with: "bigbang@mail.com"
28-
fill_in "user_password", with: @password
29-
fill_in "user_password_confirmation", with: @password
3027
end
3128

3229
it "should create new user" do
@@ -57,26 +54,13 @@
5754
end
5855

5956
it "should send valid email to user with email & password" do
60-
Gitlab.config.gitlab.stub(:signup_enabled).and_return(false)
6157
User.observers.enable :user_observer do
6258
click_button "Create user"
6359
user = User.last
6460
email = ActionMailer::Base.deliveries.last
6561
email.subject.should have_content("Account was created")
6662
email.text_part.body.should have_content(user.email)
67-
email.text_part.body.should have_content(@password)
68-
end
69-
end
70-
71-
it "should send valid email to user with email without password when signup is enabled" do
72-
Gitlab.config.gitlab.stub(:signup_enabled).and_return(true)
73-
User.observers.enable :user_observer do
74-
click_button "Create user"
75-
user = User.last
76-
email = ActionMailer::Base.deliveries.last
77-
email.subject.should have_content("Account was created")
78-
email.text_part.body.should have_content(user.email)
79-
email.text_part.body.should_not have_content(@password)
63+
email.text_part.body.should have_content('password')
8064
end
8165
end
8266
end

0 commit comments

Comments
 (0)