Commit 47234ab3672697f85f8e9f452aea4453e1b9aadc

Authored by Dmitriy Zaporozhets
1 parent 5ed4e7e2

Validate username. Gitlab::Regex added

app/models/namespace.rb
@@ -19,7 +19,7 @@ class Namespace < ActiveRecord::Base @@ -19,7 +19,7 @@ class Namespace < ActiveRecord::Base
19 19
20 validates :name, presence: true, uniqueness: true 20 validates :name, presence: true, uniqueness: true
21 validates :path, uniqueness: true, presence: true, length: { within: 1..255 }, 21 validates :path, uniqueness: true, presence: true, length: { within: 1..255 },
22 - format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/, 22 + format: { with: Gitlab::Regex.path_regex,
23 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } 23 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
24 validates :owner, presence: true 24 validates :owner, presence: true
25 25
app/models/project.rb
@@ -59,7 +59,7 @@ class Project < ActiveRecord::Base @@ -59,7 +59,7 @@ class Project < ActiveRecord::Base
59 validates :description, length: { within: 0..2000 } 59 validates :description, length: { within: 0..2000 }
60 validates :name, presence: true, length: { within: 0..255 } 60 validates :name, presence: true, length: { within: 0..255 }
61 validates :path, presence: true, length: { within: 0..255 }, 61 validates :path, presence: true, length: { within: 0..255 },
62 - format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/, 62 + format: { with: Gitlab::Regex.path_regex,
63 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } 63 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
64 validates :issues_enabled, :wall_enabled, :merge_requests_enabled, 64 validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
65 :wiki_enabled, inclusion: { in: [true, false] } 65 :wiki_enabled, inclusion: { in: [true, false] }
app/models/user.rb
@@ -41,8 +41,8 @@ class User < ActiveRecord::Base @@ -41,8 +41,8 @@ class User < ActiveRecord::Base
41 41
42 attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, :name, :username, 42 attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, :name, :username,
43 :skype, :linkedin, :twitter, :dark_scheme, :theme_id, :force_random_password, 43 :skype, :linkedin, :twitter, :dark_scheme, :theme_id, :force_random_password,
44 - :extern_uid, :provider, :as => [:default, :admin]  
45 - attr_accessible :projects_limit, :as => :admin 44 + :extern_uid, :provider, as: [:default, :admin]
  45 + attr_accessible :projects_limit, as: :admin
46 46
47 attr_accessor :force_random_password 47 attr_accessor :force_random_password
48 48
@@ -63,9 +63,12 @@ class User < ActiveRecord::Base @@ -63,9 +63,12 @@ class User < ActiveRecord::Base
63 has_many :assigned_merge_requests, class_name: "MergeRequest", foreign_key: :assignee_id, dependent: :destroy 63 has_many :assigned_merge_requests, class_name: "MergeRequest", foreign_key: :assignee_id, dependent: :destroy
64 64
65 validates :bio, length: { within: 0..255 } 65 validates :bio, length: { within: 0..255 }
66 - validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider} 66 + validates :extern_uid, allow_blank: true, uniqueness: {scope: :provider}
67 validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0} 67 validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
68 - validates :username, presence: true 68 + validates :username, presence: true, uniqueness: true,
  69 + format: { with: Gitlab::Regex.username_regex,
  70 + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
  71 +
69 72
70 before_validation :generate_password, on: :create 73 before_validation :generate_password, on: :create
71 before_save :ensure_authentication_token 74 before_save :ensure_authentication_token
app/views/admin/users/index.html.haml
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 = form_tag admin_users_path, method: :get, class: 'form-inline' do 6 = form_tag admin_users_path, method: :get, class: 'form-inline' do
7 = text_field_tag :name, params[:name], class: "xlarge" 7 = text_field_tag :name, params[:name], class: "xlarge"
8 = submit_tag "Search", class: "btn submit primary" 8 = submit_tag "Search", class: "btn submit primary"
9 -%ul.nav.nav-pills 9 +%ul.nav.nav-tabs
10 %li{class: "#{'active' unless params[:filter]}"} 10 %li{class: "#{'active' unless params[:filter]}"}
11 = link_to "Active", admin_users_path 11 = link_to "Active", admin_users_path
12 %li{class: "#{'active' if params[:filter] == "admins"}"} 12 %li{class: "#{'active' if params[:filter] == "admins"}"}
@@ -23,24 +23,25 @@ @@ -23,24 +23,25 @@
23 %thead 23 %thead
24 %th Admin 24 %th Admin
25 %th Name 25 %th Name
  26 + %th Username
26 %th Email 27 %th Email
27 %th Projects 28 %th Projects
28 %th Edit 29 %th Edit
29 - %th Blocked  
30 %th.cred Danger Zone! 30 %th.cred Danger Zone!
31 31
32 - @admin_users.each do |user| 32 - @admin_users.each do |user|
33 %tr 33 %tr
34 %td= check_box_tag "admin", 1, user.admin, disabled: :disabled 34 %td= check_box_tag "admin", 1, user.admin, disabled: :disabled
35 %td= link_to user.name, [:admin, user] 35 %td= link_to user.name, [:admin, user]
  36 + %td= user.username
36 %td= user.email 37 %td= user.email
37 %td= user.users_projects.count 38 %td= user.users_projects.count
38 %td= link_to 'Edit', edit_admin_user_path(user), id: "edit_#{dom_id(user)}", class: "btn small" 39 %td= link_to 'Edit', edit_admin_user_path(user), id: "edit_#{dom_id(user)}", class: "btn small"
39 - %td 40 + %td.bgred
40 - if user.blocked 41 - if user.blocked
41 = link_to 'Unblock', unblock_admin_user_path(user), method: :put, class: "btn small success" 42 = link_to 'Unblock', unblock_admin_user_path(user), method: :put, class: "btn small success"
42 - else 43 - else
43 = link_to 'Block', block_admin_user_path(user), confirm: 'USER WILL BE BLOCKED! Are you sure?', method: :put, class: "btn small danger" 44 = link_to 'Block', block_admin_user_path(user), confirm: 'USER WILL BE BLOCKED! Are you sure?', method: :put, class: "btn small danger"
44 - %td.bgred= link_to 'Destroy', [:admin, user], confirm: "USER #{user.name} WILL BE REMOVED! Are you sure?", method: :delete, class: "btn small danger" 45 + = link_to 'Destroy', [:admin, user], confirm: "USER #{user.name} WILL BE REMOVED! Are you sure?", method: :delete, class: "btn small danger"
45 46
46 = paginate @admin_users, theme: "admin" 47 = paginate @admin_users, theme: "admin"
app/views/admin/users/show.html.haml
@@ -24,6 +24,12 @@ @@ -24,6 +24,12 @@
24 %tr 24 %tr
25 %td 25 %td
26 %b 26 %b
  27 + Username:
  28 + %td
  29 + = @admin_user.username
  30 + %tr
  31 + %td
  32 + %b
27 Admin: 33 Admin:
28 %td= check_box_tag "admin", 1, @admin_user.admin, disabled: :disabled 34 %td= check_box_tag "admin", 1, @admin_user.admin, disabled: :disabled
29 %tr 35 %tr
app/views/profile/account.html.haml
@@ -42,11 +42,11 @@ @@ -42,11 +42,11 @@
42 42
43 .clearfix 43 .clearfix
44 = f.label :password 44 = f.label :password
45 - .input= f.password_field :password 45 + .input= f.password_field :password, required: true
46 .clearfix 46 .clearfix
47 = f.label :password_confirmation 47 = f.label :password_confirmation
48 .input 48 .input
49 - = f.password_field :password_confirmation 49 + = f.password_field :password_confirmation, required: true
50 .clearfix 50 .clearfix
51 .input 51 .input
52 = f.submit 'Save password', class: "btn save-btn" 52 = f.submit 'Save password', class: "btn save-btn"
@@ -62,7 +62,7 @@ @@ -62,7 +62,7 @@
62 .padded 62 .padded
63 = f.label :username 63 = f.label :username
64 .input 64 .input
65 - = f.text_field :username 65 + = f.text_field :username, required: true
66 .input 66 .input
67 = f.submit 'Save username', class: "btn save-btn" 67 = f.submit 'Save username', class: "btn save-btn"
68 68
lib/gitlab/regex.rb 0 → 100644
@@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
  1 +module Gitlab
  2 + module Regex
  3 + extend self
  4 +
  5 + def username_regex
  6 + default_regex
  7 + end
  8 +
  9 + def path_regex
  10 + default_regex
  11 + end
  12 +
  13 + protected
  14 +
  15 + def default_regex
  16 + /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/
  17 + end
  18 + end
  19 +end
spec/models/user_spec.rb
@@ -58,6 +58,7 @@ describe User do @@ -58,6 +58,7 @@ describe User do
58 end 58 end
59 59
60 describe 'validations' do 60 describe 'validations' do
  61 + it { should validate_presence_of(:username) }
61 it { should validate_presence_of(:projects_limit) } 62 it { should validate_presence_of(:projects_limit) }
62 it { should validate_numericality_of(:projects_limit) } 63 it { should validate_numericality_of(:projects_limit) }
63 it { should allow_value(0).for(:projects_limit) } 64 it { should allow_value(0).for(:projects_limit) }