Skip to content

Commit

Permalink
Augment user information storage api, separate username from email ha…
Browse files Browse the repository at this point in the history
…ndling
  • Loading branch information
guerler committed Apr 25, 2018
1 parent 7f5206a commit 5c6704b
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/galaxy/webapps/galaxy/api/users.py
Expand Up @@ -317,7 +317,7 @@ def _build_extra_user_pref_inputs(self, preferences, user):
@expose_api
def get_information(self, trans, id, **kwd):
"""
GET /api/users/{id}/information
GET /api/users/{id}/information/inputs
Return user details such as username, email, addresses etc.
:param id: the encoded id of the user
Expand Down Expand Up @@ -400,7 +400,7 @@ def get_information(self, trans, id, **kwd):
@expose_api
def set_information(self, trans, id, payload={}, **kwd):
"""
POST /api/users/{id}/information
PUT /api/users/{id}/information/inputs
Save a user's email, username, addresses etc.
:param id: the encoded id of the user
Expand All @@ -410,12 +410,10 @@ def set_information(self, trans, id, payload={}, **kwd):
:type payload: dict
"""
user = self._get_user(trans, id)
email = payload.get('email')
username = payload.get('username')
if email or username:
message = self._validate_email_publicname(email, username) or validate_email(trans, email, user)
if not message and username:
message = validate_publicname(trans, username, user)
# Update email
if 'email' in payload:
email = payload.get('email')
message = self._validate_email(email) or validate_email(trans, email, user)
if message:
raise MessageException(message)
if user.email != email:
Expand All @@ -437,8 +435,13 @@ def set_information(self, trans, id, payload={}, **kwd):
if trans.app.config.error_email_to is not None:
message += ' Contact: %s' % trans.app.config.error_email_to
raise MessageException(message)
# Update public name
if 'username' in payload:
username = payload.get('username')
message = self._validate_publicname(username) or validate_publicname(trans, username, user)
if message:
raise MessageException(message)
if user.username != username:
# Update public name
user.username = username
# Update user custom form
user_info_form_id = payload.get('info|form_id')
Expand Down Expand Up @@ -561,17 +564,19 @@ def get_activation_token(self, trans, email):
trans.sa_session.flush()
return activation_token

def _validate_email_publicname(self, email, username):
def _validate_email(self, email):
''' Validate email and username using regex '''
if email == '' or not isinstance(email, six.string_types):
return 'Please provide your email address.'
if not re.match('^[a-z0-9\-]{3,255}$', username):
return 'Public name must contain only lowercase letters, numbers and "-". It also has to be shorter than 255 characters but longer than 2.'
if not re.match('^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$', email):
return 'Please provide your valid email address.'
if len(email) > 255:
return 'Email cannot be more than 255 characters in length.'

def _validate_publicname(self, username):
if not re.match('^[a-z0-9\-]{3,255}$', username):
return 'Public name must contain only lowercase letters, numbers and "-". It also has to be shorter than 255 characters but longer than 2.'

@expose_api
def get_password(self, trans, id, payload={}, **kwd):
"""
Expand Down

0 comments on commit 5c6704b

Please sign in to comment.