Move users to separate module

This commit is contained in:
Sunil Mohan Adapa committed 2014-06-01 23:35:11 +05:30
1 parent 1aaa930a1f
commit ef01d89b0b
4 files changed
+1 -1

No files matched your search

@@ -0,0 +1,52 @@
{% extends "login_nav.html" %}
{% comment %}
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% block main_block %}
{% include 'messages.html' %}
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn-primary" value="Add User"/>
</form>
{% endblock %}
{% block sidebar_right_block %}
<div class="well sidebar-nav">
<h3>Add User</h3>
<p>Adding a user via this administrative
interface <strong>might</strong> create a system user. For
example, if you provide a user with ssh access, she will need a
system account. If you don't know what that means, don't worry
about it.</p>
</div>
{% endblock %}
@@ -0,0 +1,57 @@
{% extends "login_nav.html" %}
{% comment %}
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% block main_block %}
{% include 'messages.html' %}
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn-primary" value="Delete User"/>
</form>
{% endblock %}
{% block sidebar_right_block %}
<div class="well sidebar-nav">
<h3>Edit Users</h3>
<p>Click on a user's name to go to a screen for editing that
user's account.</p>
<h3>Delete Users</h3>
<p>Check the box next to a users' names and then click "Delete
User" to remove users from {{ cfg.product_name }} and the
{{ cfg.box_name }} system.</p>
<p>Deleting users is permanent!</p>
</div>
{% endblock %}
+172
View File
@@ -0,0 +1,172 @@
import cherrypy
from django import forms
from django.core import validators
from gettext import gettext as _
import auth
from auth import require
from plugin_mount import PagePlugin
import cfg
from model import User
import util
class Users(PagePlugin):
order = 20 # order of running init in PagePlugins
def __init__(self, *args, **kwargs):
PagePlugin.__init__(self, *args, **kwargs)
self.register_page("sys.users")
@staticmethod
@cherrypy.expose
@require()
def index():
"""Return a rendered users page"""
menu = {'title': _('Users and Groups'),
'items': [{'url': '/sys/users/add',
'text': _('Add User')},
{'url': '/sys/users/edit',
'text': _('Edit Users')}]}
sidebar_right = util.render_template(template='menu_block',
menu=menu)
return util.render_template(title="Manage Users and Groups",
sidebar_right=sidebar_right)
class UserAddForm(forms.Form): # pylint: disable-msg=W0232
"""Form to add a new user"""
username = forms.CharField(
label=_('Username'),
help_text=_('Must be lower case alphanumeric and start with \
and alphabet'),
validators=[
validators.RegexValidator(r'^[a-z][a-z0-9]*$',
_('Invalid username'))])
password = forms.CharField(label=_('Password'),
widget=forms.PasswordInput())
full_name = forms.CharField(label=_('Full name'), required=False)
email = forms.EmailField(label=_('Email'), required=False)
class UserAdd(PagePlugin):
"""Add user page"""
order = 30
def __init__(self, *args, **kwargs):
PagePlugin.__init__(self, *args, **kwargs)
self.register_page('sys.users.add')
@cherrypy.expose
@require()
def index(self, **kwargs):
"""Serve the form"""
form = None
messages = []
if kwargs:
form = UserAddForm(kwargs, prefix='user')
# pylint: disable-msg=E1101
if form.is_valid():
self._add_user(form.cleaned_data, messages)
form = UserAddForm(prefix='user')
else:
form = UserAddForm(prefix='user')
return util.render_template(template='users_add', title=_('Add User'),
form=form, messages=messages)
@staticmethod
def _add_user(data, messages):
"""Add a user"""
if cfg.users.exists(data['username']):
messages.append(
('error', _('User "{username}" already exists').format(
username=data['username'])))
return
auth.add_user(data['username'], data['password'], data['full_name'],
data['email'], False)
messages.append(
('success', _('User "{username}" added').format(
username=data['username'])))
class UserEditForm(forms.Form): # pylint: disable-msg=W0232
"""Form to edit/delete a user"""
def __init__(self, *args, **kwargs):
# pylint: disable-msg=E1002
super(forms.Form, self).__init__(*args, **kwargs)
users = cfg.users.get_all()
for uname in users:
user = User(uname[1])
label = '%s (%s)' % (user['name'], user['username'])
field = forms.BooleanField(label=label, required=False)
# pylint: disable-msg=E1101
self.fields['delete_user_' + user['username']] = field
class UserEdit(PagePlugin):
"""User edit page"""
order = 35
def __init__(self, *args, **kwargs):
PagePlugin.__init__(self, *args, **kwargs)
self.register_page('sys.users.edit')
@cherrypy.expose
@require()
def index(self, **kwargs):
"""Serve the form"""
form = None
messages = []
if kwargs:
form = UserEditForm(kwargs, prefix='user')
# pylint: disable-msg=E1101
if form.is_valid():
self._apply_changes(form.cleaned_data, messages)
form = UserEditForm(prefix='user')
else:
form = UserEditForm(prefix='user')
return util.render_template(template='users_edit',
title=_('Edit or Delete User'),
form=form, messages=messages)
@staticmethod
def _apply_changes(data, messages):
"""Apply form changes"""
for field, value in data.items():
if not value:
continue
if not field.startswith('delete_user_'):
continue
username = field.split('delete_user_')[1]
cfg.log.info('%s asked to delete %s' %
(cherrypy.session.get(cfg.session_key), username))
if username == cfg.users.current(name=True):
messages.append(
('error',
_('Can not delete current account - "%s"') % username))
continue
if not cfg.users.exists(username):
messages.append(('error',
_('User "%s" does not exist') % username))
continue
try:
cfg.users.remove(username)
messages.append(('success', _('User "%s" deleted') % username))
except IOError as exception:
messages.append(('error', _('Error deleting "%s" - %s') %
(username, exception)))