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 19  
20 20 validates :name, presence: true, uniqueness: true
21 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 23 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
24 24 validates :owner, presence: true
25 25  
... ...
app/models/project.rb
... ... @@ -59,7 +59,7 @@ class Project < ActiveRecord::Base
59 59 validates :description, length: { within: 0..2000 }
60 60 validates :name, presence: true, length: { within: 0..255 }
61 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 63 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
64 64 validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
65 65 :wiki_enabled, inclusion: { in: [true, false] }
... ...
app/models/user.rb
... ... @@ -41,8 +41,8 @@ class User < ActiveRecord::Base
41 41  
42 42 attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, :name, :username,
43 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 47 attr_accessor :force_random_password
48 48  
... ... @@ -63,9 +63,12 @@ class User < ActiveRecord::Base
63 63 has_many :assigned_merge_requests, class_name: "MergeRequest", foreign_key: :assignee_id, dependent: :destroy
64 64  
65 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 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 73 before_validation :generate_password, on: :create
71 74 before_save :ensure_authentication_token
... ...
app/views/admin/users/index.html.haml
... ... @@ -6,7 +6,7 @@
6 6 = form_tag admin_users_path, method: :get, class: 'form-inline' do
7 7 = text_field_tag :name, params[:name], class: "xlarge"
8 8 = submit_tag "Search", class: "btn submit primary"
9   -%ul.nav.nav-pills
  9 +%ul.nav.nav-tabs
10 10 %li{class: "#{'active' unless params[:filter]}"}
11 11 = link_to "Active", admin_users_path
12 12 %li{class: "#{'active' if params[:filter] == "admins"}"}
... ... @@ -23,24 +23,25 @@
23 23 %thead
24 24 %th Admin
25 25 %th Name
  26 + %th Username
26 27 %th Email
27 28 %th Projects
28 29 %th Edit
29   - %th Blocked
30 30 %th.cred Danger Zone!
31 31  
32 32 - @admin_users.each do |user|
33 33 %tr
34 34 %td= check_box_tag "admin", 1, user.admin, disabled: :disabled
35 35 %td= link_to user.name, [:admin, user]
  36 + %td= user.username
36 37 %td= user.email
37 38 %td= user.users_projects.count
38 39 %td= link_to 'Edit', edit_admin_user_path(user), id: "edit_#{dom_id(user)}", class: "btn small"
39   - %td
  40 + %td.bgred
40 41 - if user.blocked
41 42 = link_to 'Unblock', unblock_admin_user_path(user), method: :put, class: "btn small success"
42 43 - else
43 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 47 = paginate @admin_users, theme: "admin"
... ...
app/views/admin/users/show.html.haml
... ... @@ -24,6 +24,12 @@
24 24 %tr
25 25 %td
26 26 %b
  27 + Username:
  28 + %td
  29 + = @admin_user.username
  30 + %tr
  31 + %td
  32 + %b
27 33 Admin:
28 34 %td= check_box_tag "admin", 1, @admin_user.admin, disabled: :disabled
29 35 %tr
... ...
app/views/profile/account.html.haml
... ... @@ -42,11 +42,11 @@
42 42  
43 43 .clearfix
44 44 = f.label :password
45   - .input= f.password_field :password
  45 + .input= f.password_field :password, required: true
46 46 .clearfix
47 47 = f.label :password_confirmation
48 48 .input
49   - = f.password_field :password_confirmation
  49 + = f.password_field :password_confirmation, required: true
50 50 .clearfix
51 51 .input
52 52 = f.submit 'Save password', class: "btn save-btn"
... ... @@ -62,7 +62,7 @@
62 62 .padded
63 63 = f.label :username
64 64 .input
65   - = f.text_field :username
  65 + = f.text_field :username, required: true
66 66 .input
67 67 = f.submit 'Save username', class: "btn save-btn"
68 68  
... ...
lib/gitlab/regex.rb 0 → 100644
... ... @@ -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 58 end
59 59  
60 60 describe 'validations' do
  61 + it { should validate_presence_of(:username) }
61 62 it { should validate_presence_of(:projects_limit) }
62 63 it { should validate_numericality_of(:projects_limit) }
63 64 it { should allow_value(0).for(:projects_limit) }
... ...