Skip to content

Commit

Permalink
[AdminTL#83] server cmd character: fix permission when get user
Browse files Browse the repository at this point in the history
- check if get user is the same user
- check admin persmission from generic command
  • Loading branch information
mathben committed Mar 19, 2018
1 parent 0796231 commit b909e30
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/web/base_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def get_current_user(self):
else:
print("Error type on cookie %s %s" % (data, self.request.remote_ip), file=sys.stderr)

def is_permission_admin(self):
return self.current_user and self.current_user.get("permission") == "Admin"

def is_user_id(self, user_id):
return self.current_user and self.current_user.get("user_id") == user_id

def give_cookie(self, user_id, twitter_access_token=None, facebook_access_token=None, google_access_token=None):
if user_id:
data = {
Expand Down
25 changes: 20 additions & 5 deletions src/web/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def get(self):
self.set_status(404)
self.send_error(404)
raise tornado.web.Finish()
if self.current_user.get("permission") == "Admin":
if self.is_permission_admin():
self.render('admin/news.html', **self._global_arg)
else:
print("Insufficient permissions from %s" % self.request.remote_ip, file=sys.stderr)
Expand All @@ -369,7 +369,7 @@ def get(self):
self.set_status(404)
self.send_error(404)
raise tornado.web.Finish()
if self.current_user.get("permission") == "Admin":
if self.is_permission_admin():
self.render('admin/character.html', **self._global_arg)
else:
print("Insufficient permissions from %s" % self.request.remote_ip, file=sys.stderr)
Expand Down Expand Up @@ -416,6 +416,7 @@ def get(self):
self.send_error(404)
raise tornado.web.Finish()

# validate argument
user_id = self.request.query[len("user_id="):]
is_admin = self.request.query == "is_admin"
if user_id == "" and not is_admin:
Expand All @@ -424,11 +425,25 @@ def get(self):
self.send_error(403)
raise tornado.web.Finish()

# TODO manage what we get and user management permission
# validate permission and send result
if is_admin:
data = json.dumps(self._db.get_all_user())
if self.is_permission_admin():
data = json.dumps(self._db.get_all_user())
else:
print("Insufficient permissions from %s" % self.request.remote_ip, file=sys.stderr)
# Forbidden
self.set_status(403)
self.send_error(403)
raise tornado.web.Finish()
else:
data = json.dumps(self._db.get_all_user(user_id=user_id))
if self.is_permission_admin() or self.is_user_id(user_id):
data = json.dumps(self._db.get_all_user(user_id=user_id))
else:
print("Insufficient permissions from %s" % self.request.remote_ip, file=sys.stderr)
# Forbidden
self.set_status(403)
self.send_error(403)
raise tornado.web.Finish()

self.write(data)
self.finish()
Expand Down

0 comments on commit b909e30

Please sign in to comment.