Skip to content

Commit

Permalink
enforce username requirements for CSV uploads (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdzim authored and D3f0 committed Sep 14, 2018
1 parent c670a97 commit 21d673a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,7 @@ span.error-highlight {
vertical-align: middle;
margin-left: 10px;
text-shadow: 0 0 1px #000;
white-space: normal;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h2 class="page-header"><i class="ss-icon">&#x1F464;</i>Add User
<form action="{% url 'blue_mgnt:users' %}" enctype="multipart/form-data" method="POST">
{% if new_user.non_field_errors %}
<span class="error-highlight">
<span class="error-tag">{{ new_user.non_field_errors.0 }}</span>
<div class="error-tag">{{ new_user.non_field_errors.0 }}</div>
</span>
{% endif %}
{% csrf_token %}
Expand All @@ -26,7 +26,7 @@ <h2 class="page-header"><i class="ss-icon">&#x1F464;</i>Add User
{% for error in field.errors %}
<span class="error-highlight">
{{ field }}
<span class="error-tag">{{ error }}</span>
<div class="error-tag">{{ error }}</div>
</span>
{% endfor %}
{% else %}
Expand Down Expand Up @@ -69,7 +69,7 @@ <h2 class="page-header"><i class="ss-icon">&#x1F464;</i>Add User
{% for error in field.errors %}
<span class="error-highlight">
{{ field }}
<span class="error-tag">{{ error }}</span>
<div class="error-tag">{{ error }}</div>
</span>
{% endfor %}
{% else %}
Expand Down
8 changes: 8 additions & 0 deletions django/apps/blue_management/blue_mgnt/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ def _csv_create_users(api, account_info, groups, config, request, csv_data):
if not group:
msg = 'Invalid data in row %s. Invalid Group' % x
return x, forms.ValidationError(msg)
if not new_user_value_re_tests['avatar']['username'].match(row['name']):
return x, forms.ValidationError(
'Invalid data in row %s. Invalid Username. ' % x +
'Usernames must start with a letter, '
'be at least four characters long, '
'and may contain letters, numbers, '
'and underscores.'
)
group_id = group['group_id']
config_group = get_config_group(config, group_id)
if config_group['user_source'] != 'local':
Expand Down
9 changes: 7 additions & 2 deletions django/apps/blue_management/blue_mgnt/views/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import re

name_validator = RegexValidator(
regex=r"^[\w\d\_]+$",
message="This field can only contain letter, numbers and underscores",
regex=r'^[a-zA-Z][a-zA-Z0-9_]{3,37}$',
message=(
'Usernames must start with a letter, '
'be at least four characters long, '
'and may contain letters, numbers, '
'and underscores.'
),
flags=re.UNICODE,
)

0 comments on commit 21d673a

Please sign in to comment.