Permalink
Browse files

Merge pull request #66 from rmartinjak/usermgmt

user management enhancements
  • Loading branch information...
2 parents 150ac05 + c65fccb commit 0ae273b251e8020afb012b0817381a5913644545 @jasongrout jasongrout committed Nov 2, 2012
Showing with 69 additions and 23 deletions.
  1. +43 −17 flask_version/admin.py
  2. +16 −4 sagenb/data/sage/html/settings/user_management.html
  3. +10 −2 sagenb/notebook/user.py
View
@@ -6,33 +6,23 @@
admin = Module('flask_version.admin')
-# '/users' does not work, because current template calls urls like '/users/?reset=...'
-@admin.route('/users/')
+@admin.route('/users')
+@admin.route('/users/reset/<reset>')
@admin_required
@with_lock
-def users():
+def users(reset=None):
template_dict = {}
-
- if 'reset' in request.values:
- user = request.values['reset']
+ if reset:
from random import choice
import string
chara = string.letters + string.digits
password = ''.join([choice(chara) for i in range(8)])
try:
- U = g.notebook.user_manager().user(user)
- g.notebook.user_manager().set_password(user, password)
- except KeyError:
- pass
- template_dict['reset'] = [user, password]
-
- if 'suspension' in request.values:
- user = request.values['suspension']
- try:
- U = g.notebook.user_manager().user(user)
- U.set_suspension()
+ U = g.notebook.user_manager().user(reset)
+ g.notebook.user_manager().set_password(reset, password)
except KeyError:
pass
+ template_dict['reset'] = [reset, password]
template_dict['number_of_users'] = len(g.notebook.user_manager().valid_login_names()) if len(g.notebook.user_manager().valid_login_names()) > 1 else None
users = sorted(g.notebook.user_manager().valid_login_names())
@@ -42,6 +32,42 @@ def users():
template_dict['username'] = g.username
return render_template(os.path.join('html', 'settings', 'user_management.html'), **template_dict)
+@admin.route('/users/suspend/<user>')
+@admin_required
+@with_lock
+def suspend_user(user):
+ try:
+ U = g.notebook.user_manager().user(user)
+ U.set_suspension()
+ except KeyError:
+ pass
+ return redirect(url_for("users"))
+
+@admin.route('/users/delete/<user>')
+@admin_required
+@with_lock
+def del_user(user):
+ if user != 'admin':
+ try:
+ g.notebook.user_manager().delete_user(user)
+ except KeyError:
+ pass
+ return redirect(url_for("users"))
+
+@admin.route('/users/toggleadmin/<user>')
+@admin_required
+@with_lock
+def toggle_admin(user):
+ try:
+ U = g.notebook.user_manager().user(user)
+ if U.is_admin():
+ U.revoke_admin()
+ else:
+ U.grant_admin()
+ except KeyError:
+ pass
+ return redirect(url_for("users"))
+
@admin.route('/adduser', methods = ['GET','POST'])
@admin_required
@with_lock
@@ -10,10 +10,22 @@
<p>{{ gettext('The password for the user %(u)s has been reset to <strong>%(p)s</strong>', u=reset[0], p=reset[1]) }}</p>
{% endif %}
<table>
- <tr><th>{{ gettext('Users') }}</th><th>{{ gettext('Password') }}</th><th>{{ gettext('Suspension') }}</th></tr>
- {% for user in users %}
- {% if user != 'admin' %}
- <tr><td><a href="/home/{{ user }}/">{{ user }}</a></td><td><a href="/users/?reset={{ user }}">{{ gettext('Reset') }}</a></td><td><a href="/users/?suspension={{ user }}">{% if user.is_suspended() %}{{ gettext('Unsuspend') }}{% else %}{{ gettext('Suspend') }}{% endif %}</td></tr>
+ <tr>
+ <th>{{ gettext('User') }}</th>
+ <th>{{ gettext('Password') }}</th>
+ <th>{{ gettext('Suspension') }}</th>
+ <th>{{ gettext('Admin') }}</th>
+ <th>{{ gettext('Delete') }}</th>
+ </tr>
+ {% for u in users %}
+ {% if u.username() != 'admin' %}
+ <tr>
+ <td><a href="/home/{{ u }}/">{{ u }}</a></td>
+ <td><a href="{{ url_for('users', reset=u.username()) }}">{{ gettext('Reset') }}</a></td>
+ <td><a href="{{ url_for('suspend_user', user=u.username()) }}">{% if u.is_suspended() %}{{ gettext('Unsuspend') }}{% else %}{{ gettext('Suspend') }}{% endif %}</a></td>
+ <td><a href="{{ url_for('toggle_admin', user=u.username()) }}">{% if u.is_admin() %}{{ gettext('Revoke') }}{% else %}{{ gettext('Grant') }}{% endif %}</a> </td>
+ <td><a href="{{ url_for('del_user', user=u.username()) }} ">{{ gettext('Delete') }}</a></td>
+ </tr>
{% endif %}
{% endfor %}
</table>
View
@@ -276,7 +276,15 @@ def is_admin(self):
sage: User('B', account_type='user').is_admin()
False
"""
- return self._account_type == 'admin'
+ return self.account_type() == 'admin'
+
+ def grant_admin(self):
+ if not self.is_guest():
+ self._account_type = 'admin'
+
+ def revoke_admin(self):
+ if not self.is_guest():
+ self._account_type = 'user'
def is_guest(self):
"""
@@ -288,7 +296,7 @@ def is_guest(self):
sage: User('B', account_type='user').is_guest()
False
"""
- return self._account_type == 'guest'
+ return self.account_type() == 'guest'
def is_suspended(self):
"""

0 comments on commit 0ae273b

Please sign in to comment.