Commit f22488f7262145c1e318db15c0af2091e67d5007
Exists in
master
and in
4 other branches
Merge pull request #3143 from Undev/state-machine-3
State machine 3
Showing
15 changed files
with
67 additions
and
32 deletions
Show diff stats
app/controllers/admin/users_controller.rb
... | ... | @@ -45,7 +45,7 @@ class Admin::UsersController < Admin::ApplicationController |
45 | 45 | end |
46 | 46 | |
47 | 47 | def unblock |
48 | - if admin_user.update_attribute(:blocked, false) | |
48 | + if admin_user.activate | |
49 | 49 | redirect_to :back, alert: "Successfully unblocked" |
50 | 50 | else |
51 | 51 | redirect_to :back, alert: "Error occured. User was not unblocked" | ... | ... |
app/controllers/application_controller.rb
... | ... | @@ -30,7 +30,7 @@ class ApplicationController < ActionController::Base |
30 | 30 | end |
31 | 31 | |
32 | 32 | def reject_blocked! |
33 | - if current_user && current_user.blocked | |
33 | + if current_user && current_user.blocked? | |
34 | 34 | sign_out current_user |
35 | 35 | flash[:alert] = "Your account is blocked. Retry when an admin unblock it." |
36 | 36 | redirect_to new_user_session_path |
... | ... | @@ -38,7 +38,7 @@ class ApplicationController < ActionController::Base |
38 | 38 | end |
39 | 39 | |
40 | 40 | def after_sign_in_path_for resource |
41 | - if resource.is_a?(User) && resource.respond_to?(:blocked) && resource.blocked | |
41 | + if resource.is_a?(User) && resource.respond_to?(:blocked?) && resource.blocked? | |
42 | 42 | sign_out resource |
43 | 43 | flash[:alert] = "Your account is blocked. Retry when an admin unblock it." |
44 | 44 | new_user_session_path | ... | ... |
app/models/user.rb
... | ... | @@ -25,7 +25,7 @@ |
25 | 25 | # dark_scheme :boolean default(FALSE), not null |
26 | 26 | # theme_id :integer default(1), not null |
27 | 27 | # bio :string(255) |
28 | -# blocked :boolean default(FALSE), not null | |
28 | +# state :string(255) | |
29 | 29 | # failed_attempts :integer default(0) |
30 | 30 | # locked_at :datetime |
31 | 31 | # extern_uid :string(255) |
... | ... | @@ -110,10 +110,27 @@ class User < ActiveRecord::Base |
110 | 110 | |
111 | 111 | delegate :path, to: :namespace, allow_nil: true, prefix: true |
112 | 112 | |
113 | + state_machine :state, initial: :active do | |
114 | + after_transition any => :blocked do |user, transition| | |
115 | + # Remove user from all projects and | |
116 | + user.users_projects.find_each do |membership| | |
117 | + return false unless membership.destroy | |
118 | + end | |
119 | + end | |
120 | + | |
121 | + event :block do | |
122 | + transition active: :blocked | |
123 | + end | |
124 | + | |
125 | + event :activate do | |
126 | + transition blocked: :active | |
127 | + end | |
128 | + end | |
129 | + | |
113 | 130 | # Scopes |
114 | 131 | scope :admins, -> { where(admin: true) } |
115 | - scope :blocked, -> { where(blocked: true) } | |
116 | - scope :active, -> { where(blocked: false) } | |
132 | + scope :blocked, -> { with_state(:blocked) } | |
133 | + scope :active, -> { with_state(:active) } | |
117 | 134 | scope :alphabetically, -> { order('name ASC') } |
118 | 135 | scope :in_team, ->(team){ where(id: team.member_ids) } |
119 | 136 | scope :not_in_team, ->(team){ where('users.id NOT IN (:ids)', ids: team.member_ids) } |
... | ... | @@ -283,17 +300,6 @@ class User < ActiveRecord::Base |
283 | 300 | MergeRequest.cared(self) |
284 | 301 | end |
285 | 302 | |
286 | - # Remove user from all projects and | |
287 | - # set blocked attribute to true | |
288 | - def block | |
289 | - users_projects.find_each do |membership| | |
290 | - return false unless membership.destroy | |
291 | - end | |
292 | - | |
293 | - self.blocked = true | |
294 | - save | |
295 | - end | |
296 | - | |
297 | 303 | def projects_limit_percent |
298 | 304 | return 100 if projects_limit.zero? |
299 | 305 | (personal_projects.count.to_f / projects_limit) * 100 | ... | ... |
app/views/admin/users/_form.html.haml
... | ... | @@ -61,7 +61,7 @@ |
61 | 61 | .span4 |
62 | 62 | - unless @admin_user.new_record? |
63 | 63 | .alert.alert-error |
64 | - - if @admin_user.blocked | |
64 | + - if @admin_user.blocked? | |
65 | 65 | %p This user is blocked and is not able to login to GitLab |
66 | 66 | = link_to 'Unblock User', unblock_admin_user_path(@admin_user), method: :put, class: "btn btn-small" |
67 | 67 | - else | ... | ... |
app/views/admin/users/index.html.haml
... | ... | @@ -53,7 +53,7 @@ |
53 | 53 | |
54 | 54 | = link_to 'Edit', edit_admin_user_path(user), id: "edit_#{dom_id(user)}", class: "btn btn-small" |
55 | 55 | - unless user == current_user |
56 | - - if user.blocked | |
56 | + - if user.blocked? | |
57 | 57 | = link_to 'Unblock', unblock_admin_user_path(user), method: :put, class: "btn btn-small success" |
58 | 58 | - else |
59 | 59 | = link_to 'Block', block_admin_user_path(user), confirm: 'USER WILL BE BLOCKED! Are you sure?', method: :put, class: "btn btn-small btn-remove" | ... | ... |
app/views/admin/users/show.html.haml
app/views/team_members/_team_member.html.haml
... | ... | @@ -20,7 +20,7 @@ |
20 | 20 | %span.label This is you! |
21 | 21 | - if @project.namespace_owner == user |
22 | 22 | %span.label Owner |
23 | - - elsif user.blocked | |
23 | + - elsif user.blocked? | |
24 | 24 | %span.label Blocked |
25 | 25 | - elsif allow_admin |
26 | 26 | = link_to project_team_member_path(@project, user), confirm: remove_from_project_team_message(@project, user), method: :delete, class: "btn-tiny btn btn-remove" do | ... | ... |
app/views/teams/members/_show.html.haml
... | ... | @@ -23,7 +23,7 @@ |
23 | 23 | %span.btn.disabled This is you! |
24 | 24 | - if @team.owner == user |
25 | 25 | %span.btn.disabled Owner |
26 | - - elsif user.blocked | |
26 | + - elsif user.blocked? | |
27 | 27 | %span.btn.disabled.blocked Blocked |
28 | 28 | - elsif allow_admin |
29 | 29 | = link_to team_member_path(@team, user), confirm: remove_from_user_team_message(@team, user), method: :delete, class: "btn-tiny btn btn-remove", title: "Remove from team" do | ... | ... |
... | ... | @@ -0,0 +1,14 @@ |
1 | +class ConvertBlockedToState < ActiveRecord::Migration | |
2 | + def up | |
3 | + User.transaction do | |
4 | + User.where(blocked: true).update_all(state: :blocked) | |
5 | + User.where(blocked: false).update_all(state: :active) | |
6 | + end | |
7 | + end | |
8 | + | |
9 | + def down | |
10 | + User.transaction do | |
11 | + User.where(state: :blocked).update_all(blocked: :true) | |
12 | + end | |
13 | + end | |
14 | +end | ... | ... |
db/schema.rb
... | ... | @@ -11,7 +11,7 @@ |
11 | 11 | # |
12 | 12 | # It's strongly recommended to check this file into your version control system. |
13 | 13 | |
14 | -ActiveRecord::Schema.define(:version => 20130220133245) do | |
14 | +ActiveRecord::Schema.define(:version => 20130304105317) do | |
15 | 15 | |
16 | 16 | create_table "events", :force => true do |t| |
17 | 17 | t.string "target_type" |
... | ... | @@ -261,7 +261,6 @@ ActiveRecord::Schema.define(:version => 20130220133245) do |
261 | 261 | t.boolean "dark_scheme", :default => false, :null => false |
262 | 262 | t.integer "theme_id", :default => 1, :null => false |
263 | 263 | t.string "bio" |
264 | - t.boolean "blocked", :default => false, :null => false | |
265 | 264 | t.integer "failed_attempts", :default => 0 |
266 | 265 | t.datetime "locked_at" |
267 | 266 | t.string "extern_uid" |
... | ... | @@ -269,10 +268,10 @@ ActiveRecord::Schema.define(:version => 20130220133245) do |
269 | 268 | t.string "username" |
270 | 269 | t.boolean "can_create_group", :default => true, :null => false |
271 | 270 | t.boolean "can_create_team", :default => true, :null => false |
271 | + t.string "state" | |
272 | 272 | end |
273 | 273 | |
274 | 274 | add_index "users", ["admin"], :name => "index_users_on_admin" |
275 | - add_index "users", ["blocked"], :name => "index_users_on_blocked" | |
276 | 275 | add_index "users", ["email"], :name => "index_users_on_email", :unique => true |
277 | 276 | add_index "users", ["extern_uid", "provider"], :name => "index_users_on_extern_uid_and_provider", :unique => true |
278 | 277 | add_index "users", ["name"], :name => "index_users_on_name" | ... | ... |
lib/api/entities.rb
... | ... | @@ -2,11 +2,11 @@ module Gitlab |
2 | 2 | module Entities |
3 | 3 | class User < Grape::Entity |
4 | 4 | expose :id, :username, :email, :name, :bio, :skype, :linkedin, :twitter, |
5 | - :dark_scheme, :theme_id, :blocked, :created_at, :extern_uid, :provider | |
5 | + :dark_scheme, :theme_id, :state, :created_at, :extern_uid, :provider | |
6 | 6 | end |
7 | 7 | |
8 | 8 | class UserBasic < Grape::Entity |
9 | - expose :id, :username, :email, :name, :blocked, :created_at | |
9 | + expose :id, :username, :email, :name, :state, :created_at | |
10 | 10 | end |
11 | 11 | |
12 | 12 | class UserLogin < UserBasic | ... | ... |
lib/gitlab/auth.rb
... | ... | @@ -41,10 +41,12 @@ module Gitlab |
41 | 41 | password_confirmation: password, |
42 | 42 | projects_limit: Gitlab.config.gitlab.default_projects_limit, |
43 | 43 | }, as: :admin) |
44 | + @user.save! | |
45 | + | |
44 | 46 | if Gitlab.config.omniauth['block_auto_created_users'] && !ldap |
45 | - @user.blocked = true | |
47 | + @user.block | |
46 | 48 | end |
47 | - @user.save! | |
49 | + | |
48 | 50 | @user |
49 | 51 | end |
50 | 52 | ... | ... |
spec/models/user_spec.rb
... | ... | @@ -25,7 +25,7 @@ |
25 | 25 | # dark_scheme :boolean default(FALSE), not null |
26 | 26 | # theme_id :integer default(1), not null |
27 | 27 | # bio :string(255) |
28 | -# blocked :boolean default(FALSE), not null | |
28 | +# state :string(255) default(FALSE), not null | |
29 | 29 | # failed_attempts :integer default(0) |
30 | 30 | # locked_at :datetime |
31 | 31 | # extern_uid :string(255) |
... | ... | @@ -140,7 +140,7 @@ describe User do |
140 | 140 | |
141 | 141 | it "should block user" do |
142 | 142 | user.block |
143 | - user.blocked.should be_true | |
143 | + user.blocked?.should be_true | |
144 | 144 | end |
145 | 145 | end |
146 | 146 | |
... | ... | @@ -149,7 +149,7 @@ describe User do |
149 | 149 | User.delete_all |
150 | 150 | @user = create :user |
151 | 151 | @admin = create :user, admin: true |
152 | - @blocked = create :user, blocked: true | |
152 | + @blocked = create :user, state: :blocked | |
153 | 153 | end |
154 | 154 | |
155 | 155 | it { User.filter("admins").should == [@admin] } | ... | ... |