Commit ce527b68f07c245e887a640b6f874406bae7d0ed
Exists in
spb-stable
and in
3 other branches
Merge branch 'improve/avatar_upload_only_images' of /home/git/repositories/gitlab/gitlabhq
Showing
2 changed files
with
21 additions
and
2 deletions
Show diff stats
app/models/user.rb
| @@ -113,9 +113,8 @@ class User < ActiveRecord::Base | @@ -113,9 +113,8 @@ class User < ActiveRecord::Base | ||
| 113 | message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } | 113 | message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } |
| 114 | 114 | ||
| 115 | validates :notification_level, inclusion: { in: Notification.notification_levels }, presence: true | 115 | validates :notification_level, inclusion: { in: Notification.notification_levels }, presence: true |
| 116 | - | ||
| 117 | validate :namespace_uniq, if: ->(user) { user.username_changed? } | 116 | validate :namespace_uniq, if: ->(user) { user.username_changed? } |
| 118 | - | 117 | + validate :avatar_type, if: ->(user) { user.avatar_changed? } |
| 119 | validates :avatar, file_size: { maximum: 100.kilobytes.to_i } | 118 | validates :avatar, file_size: { maximum: 100.kilobytes.to_i } |
| 120 | 119 | ||
| 121 | before_validation :generate_password, on: :create | 120 | before_validation :generate_password, on: :create |
| @@ -244,6 +243,12 @@ class User < ActiveRecord::Base | @@ -244,6 +243,12 @@ class User < ActiveRecord::Base | ||
| 244 | end | 243 | end |
| 245 | end | 244 | end |
| 246 | 245 | ||
| 246 | + def avatar_type | ||
| 247 | + unless self.avatar.image? | ||
| 248 | + self.errors.add :avatar, "only images allowed" | ||
| 249 | + end | ||
| 250 | + end | ||
| 251 | + | ||
| 247 | # Groups user has access to | 252 | # Groups user has access to |
| 248 | def authorized_groups | 253 | def authorized_groups |
| 249 | @authorized_groups ||= begin | 254 | @authorized_groups ||= begin |
spec/models/user_spec.rb
| @@ -279,4 +279,18 @@ describe User do | @@ -279,4 +279,18 @@ describe User do | ||
| 279 | User.by_username_or_id('bar').should be_nil | 279 | User.by_username_or_id('bar').should be_nil |
| 280 | end | 280 | end |
| 281 | end | 281 | end |
| 282 | + | ||
| 283 | + describe :avatar_type do | ||
| 284 | + let(:user) { create(:user) } | ||
| 285 | + | ||
| 286 | + it "should be true if avatar is image" do | ||
| 287 | + user.update_attribute(:avatar, 'uploads/avatar.png') | ||
| 288 | + user.avatar_type.should be_true | ||
| 289 | + end | ||
| 290 | + | ||
| 291 | + it "should be false if avatar is html page" do | ||
| 292 | + user.update_attribute(:avatar, 'uploads/avatar.html') | ||
| 293 | + user.avatar_type.should == ["only images allowed"] | ||
| 294 | + end | ||
| 295 | + end | ||
| 282 | end | 296 | end |