Added support for users
This commit is contained in:
parent
646f0bb721
commit
88e2428a71
4 changed files with 220 additions and 2 deletions
|
|
@ -16,6 +16,7 @@ from paramecio2.libraries.formsutils import show_form, check_form
|
||||||
from modules.pastafari2.libraries.task import Task as SSHTask
|
from modules.pastafari2.libraries.task import Task as SSHTask
|
||||||
from modules.pastafari2.models.tasks import Task, LogTask
|
from modules.pastafari2.models.tasks import Task, LogTask
|
||||||
from modules.pastafari2.libraries.configtask import config_task
|
from modules.pastafari2.libraries.configtask import config_task
|
||||||
|
from modules.pastafari2.libraries.check_task import check_task_status
|
||||||
from modules.pastafari2.models.pastafari2 import ServerGroup, ServerDbTask, UpdateServerScripts, SystemUser
|
from modules.pastafari2.models.pastafari2 import ServerGroup, ServerDbTask, UpdateServerScripts, SystemUser
|
||||||
from paramecio2.libraries.config_admin import config_admin
|
from paramecio2.libraries.config_admin import config_admin
|
||||||
#from modules.pastafari2.settings.config_admin import pastafari_admin_i18n, pastafari_admin_i18n, pastafari_settings_i18n, pastafari_servers_i18n, pastafari_groups_i18n, pastafari_tasks_log_i18n, num_element_admin
|
#from modules.pastafari2.settings.config_admin import pastafari_admin_i18n, pastafari_admin_i18n, pastafari_settings_i18n, pastafari_servers_i18n, pastafari_groups_i18n, pastafari_tasks_log_i18n, num_element_admin
|
||||||
|
|
@ -889,7 +890,7 @@ def pastafari2_get_server_users(server_id):
|
||||||
|
|
||||||
with db.query('select * from systemuser WHERE server_id=%s', [server_id]) as cursor:
|
with db.query('select * from systemuser WHERE server_id=%s', [server_id]) as cursor:
|
||||||
for user in cursor:
|
for user in cursor:
|
||||||
rows.append({'username': user['username'], 'options': ''})
|
rows.append({'username': '<a href="#" class="change_password">'+user['username']+'</a>', 'options': ''})
|
||||||
|
|
||||||
|
|
||||||
arr_return={'fields': fields, 'rows': rows, 'html_pages': ''}
|
arr_return={'fields': fields, 'rows': rows, 'html_pages': ''}
|
||||||
|
|
@ -1010,3 +1011,59 @@ def pastafari2_add_user_task():
|
||||||
|
|
||||||
return {'error': error, 'txt_error': txt_error, 'error_form': error_form, 'task_id': task_id, 'log': arr_log}
|
return {'error': error, 'txt_error': txt_error, 'error_form': error_form, 'task_id': task_id, 'log': arr_log}
|
||||||
|
|
||||||
|
|
||||||
|
@admin_app.route('/pastafari2/change_user_password/<int:server_id>', methods=['POST'])
|
||||||
|
def pastafari2_change_user_password(server_id):
|
||||||
|
|
||||||
|
#server_id=request.args.get('webserver_id', '0')
|
||||||
|
|
||||||
|
error=0
|
||||||
|
|
||||||
|
error_form={}
|
||||||
|
|
||||||
|
task_id=0
|
||||||
|
|
||||||
|
db=g.connection
|
||||||
|
|
||||||
|
serverdb=ServerDbTask(db)
|
||||||
|
|
||||||
|
arr_server=serverdb.select_a_row(server_id, [], True)
|
||||||
|
|
||||||
|
if arr_server:
|
||||||
|
|
||||||
|
ssh_pub_key=request.form.get('ssh_pub_key', '')
|
||||||
|
|
||||||
|
user=request.form.get('user', '')
|
||||||
|
|
||||||
|
if user=='':
|
||||||
|
error_form['#user_error']=_('Error: you need an user')
|
||||||
|
error=1
|
||||||
|
|
||||||
|
if ssh_pub_key=='':
|
||||||
|
error_form['#ssh_pub_key_error']=_('Error: you need a ssh public key')
|
||||||
|
error=1
|
||||||
|
|
||||||
|
sshtask=Task(db)
|
||||||
|
|
||||||
|
ssh_key_priv=config_task.ssh_private_key
|
||||||
|
|
||||||
|
if not sshtask.run_task(arr_server['ip'], 'modules.pastafari2.tasks.system.change_password_user', 'Change user password', 'Change user password', 'Task for change unix user password', {'user': user, 'ssh_pub_key': ssh_pub_key}, config_task.remote_user, '', '', url_for('admin_app.pastafari2_dashboard', server_id=server_id), ssh_key_priv=ssh_key_priv, ssh_key_password='', send_task=True, ssh_port=arr_server['ssh_port']):
|
||||||
|
|
||||||
|
error=1
|
||||||
|
|
||||||
|
task_id=sshtask.task_id
|
||||||
|
|
||||||
|
if task_id:
|
||||||
|
|
||||||
|
log=check_task_status(_('SSH key changed'), db, task_id)
|
||||||
|
|
||||||
|
error=log['error']
|
||||||
|
|
||||||
|
error_form['#ssh_pub_key_error']=log['message']
|
||||||
|
|
||||||
|
else:
|
||||||
|
error=1
|
||||||
|
|
||||||
|
return {'error': error, 'error_form': error_form, 'task_id': task_id}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
41
libraries/check_task.py
Normal file
41
libraries/check_task.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
from time import time
|
||||||
|
from flask import flash
|
||||||
|
|
||||||
|
def check_task_status(flash_text, db, task_id):
|
||||||
|
|
||||||
|
first_time=time()
|
||||||
|
|
||||||
|
check_task=True
|
||||||
|
|
||||||
|
log={'error': 1, 'status': 1, 'message': ''}
|
||||||
|
|
||||||
|
while check_task:
|
||||||
|
|
||||||
|
with db.query('select * from logtask where task_id=%s order by id DESC limit 1', [task_id]) as cursor:
|
||||||
|
|
||||||
|
arr_log=cursor.fetchone()
|
||||||
|
|
||||||
|
if arr_log:
|
||||||
|
|
||||||
|
if arr_log['status']==1:
|
||||||
|
|
||||||
|
check_task=False
|
||||||
|
|
||||||
|
if not arr_log['error']:
|
||||||
|
flash(flash_text)
|
||||||
|
|
||||||
|
log=arr_log
|
||||||
|
|
||||||
|
return log
|
||||||
|
|
||||||
|
|
||||||
|
if time()-first_time>300:
|
||||||
|
|
||||||
|
log['message']='Error: task time out!, view task log!'
|
||||||
|
log['error']=1
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
|
return log
|
||||||
|
|
||||||
|
|
||||||
66
tasks/system/add_user.py
Normal file
66
tasks/system/add_user.py
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
#/usr/bin/env python3
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
|
import json
|
||||||
|
from modules.pastafari2.libraries.task import Task
|
||||||
|
from modules.pastafari2.models.pastafari2 import ServerDbTask, SystemUser
|
||||||
|
from modules.pastafari2.libraries.configtask import config_task
|
||||||
|
#import distro
|
||||||
|
|
||||||
|
class ServerTask(Task):
|
||||||
|
|
||||||
|
def __init__(self, server, conn, remote_user='root', remote_password='', private_key='./ssh/id_rsa', password_key='', remote_path='pastafari2', task_id=0, data={}, port=22):
|
||||||
|
|
||||||
|
super().__init__(server, conn, remote_user, remote_password, private_key, password_key, remote_path, task_id, data, port)
|
||||||
|
|
||||||
|
self.name_task='Add user to server'
|
||||||
|
|
||||||
|
self.description_task='Add user to the selected server'
|
||||||
|
|
||||||
|
self.codename_task='add_user'
|
||||||
|
|
||||||
|
self.files=[]
|
||||||
|
|
||||||
|
self.files=[['modules/pastafari2/scripts/system/add_user.py', 0o755]]
|
||||||
|
|
||||||
|
# Format first array element is command with the interpreter, the task is agnostic, the files in os directory. The commands are setted with 750 permission.
|
||||||
|
# First element is the file, next elements are the arguments
|
||||||
|
|
||||||
|
self.commands_to_execute=[['modules/pastafari2/scripts/system/add_user.py', '']]
|
||||||
|
|
||||||
|
#THe files to delete
|
||||||
|
|
||||||
|
self.delete_files=[]
|
||||||
|
|
||||||
|
self.delete_directories=['modules/pastafari2/scripts']
|
||||||
|
|
||||||
|
#self.task=Task(conn)
|
||||||
|
|
||||||
|
self.one_time=False
|
||||||
|
|
||||||
|
self.version='1.0'
|
||||||
|
|
||||||
|
self.path_module='admin_app.pastafari2_dashboard'
|
||||||
|
|
||||||
|
def pre_task(self):
|
||||||
|
|
||||||
|
|
||||||
|
self.commands_to_execute=[['/home/{}/pythonenv/bin/python3 -u modules/pastafari2/scripts/system/add_user.py'.format(config_task.remote_user), '--user={} --home={} --shell={}'.format(self.data['username'], self.data['home'], '/bin/bash')]]
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def post_task(self):
|
||||||
|
|
||||||
|
serverdb=ServerDbTask(self.connection)
|
||||||
|
|
||||||
|
system_user=SystemUser(self.connection)
|
||||||
|
|
||||||
|
arr_server=serverdb.set_conditions('WHERE ip=%s', [self.server]).select_a_row_where()
|
||||||
|
|
||||||
|
system_user.create_forms()
|
||||||
|
|
||||||
|
system_user.insert({'username': self.data['username'], 'home': self.data['home'], 'server_id': arr_server['id']})
|
||||||
|
|
||||||
|
#with self.connection.query('insert into systemuser (username, home, server_id) VALUES (%s, %s, %s)', [self.data['username'], self.data['home']]) as cursor:
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
<%inherit file="dashboard.phtml"/>
|
<%inherit file="dashboard.phtml"/>
|
||||||
|
<%block name="extra_css">
|
||||||
|
<link href="${make_media_url('js/jsutils/css/popup.css', 'pastafari2')}" rel="stylesheet" />
|
||||||
|
<link href="${make_media_url('css/popup.css', 'pastafari2')}" rel="stylesheet" />
|
||||||
|
</%block>
|
||||||
<%block name="content">
|
<%block name="content">
|
||||||
<p><a href="${url_for('admin_app.pastafari2_dashboard')}">${_('Servers')}</a> >> ${server_data['hostname']} - ${_('Users')}</p>
|
<p><a href="${url_for('admin_app.pastafari2_dashboard')}">${_('Servers')}</a> >> ${server_data['hostname']} - ${_('Users')}</p>
|
||||||
<p><a href="${url_for('admin_app.pastafari2_add_user', server_id=server_id)}">${_('Add new user to server')}</a></p>
|
<p><a href="${url_for('admin_app.pastafari2_add_user', server_id=server_id)}">${_('Add new user to server')}</a></p>
|
||||||
|
|
@ -6,13 +10,63 @@
|
||||||
</div>
|
</div>
|
||||||
<p><strong>*${_('Only can edit users system created from this control panel')}</strong></p>
|
<p><strong>*${_('Only can edit users system created from this control panel')}</strong></p>
|
||||||
<p><a href="${url_for('admin_app.pastafari2_dashboard')}">${_('Servers')}</a> >> ${server_data['hostname']} - ${_('Users')}</p>
|
<p><a href="${url_for('admin_app.pastafari2_dashboard')}">${_('Servers')}</a> >> ${server_data['hostname']} - ${_('Users')}</p>
|
||||||
|
<div id="popup_change" style="display:none;">
|
||||||
|
<div class="title title_popup" style="">
|
||||||
|
${lang('webservers', 'change_user_ssh_key', 'Change user ssh key for access')} <a href="#" class="close_icon close_popup"><i class="fa fa-window-close" aria-hidden="true"></i></a>
|
||||||
|
</div>
|
||||||
|
<div class="cont cont_popup" style="">
|
||||||
|
${_('If you want SSH access with this user, you can add a ssh pub key here.')}
|
||||||
|
<form method="post" action="" id="form_change_password">
|
||||||
|
<div class="form">
|
||||||
|
<p><label for="username">${_('Username')}:</label> <span id="username_label"></span><input type="hidden" name="user" id="user_form" value=""/></p>
|
||||||
|
<!--<p><label>User password
|
||||||
|
<i class="fa fa-question-circle tooltip" data-tooltip-content="#tooltip_password_content" style="cursor:pointer;"></i>
|
||||||
|
|
||||||
|
</label><input type="password" class="" name="password" id="password_form" value="" /> <span class="error" id="password_error"></span></p>
|
||||||
|
|
||||||
|
<div class="tooltip_templates" style="display:none;"><div id="tooltip_password_content">User password used for the user if you want access to this server using this user. <strong>Remember, the password is not saved in pastafari</strong></div></div>
|
||||||
|
|
||||||
|
<p><label>Repeat User password</label><input type="password" class="" name="repeat_password" id="repeat_password_form" value="" /> <span class="error" id="repeat_password_error"></span></p>-->
|
||||||
|
<p><label>SSH Pub Key</label><textarea name="ssh_pub_key" id="ssh_pub_key"></textarea></span><span class="error" id="ssh_pub_key_error"></span></p>
|
||||||
|
</div>
|
||||||
|
<input type="submit" value="${_('Change SSH key')}" />
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</%block>
|
</%block>
|
||||||
<%block name="jscript_block">
|
<%block name="jscript_block">
|
||||||
<script src="${make_media_url('js/jsutils/posting2.js', 'pastafari2')}"></script>
|
<script src="${make_media_url('js/jsutils/posting2.js', 'pastafari2')}"></script>
|
||||||
<script src="${make_media_url('js/jsutils/ajax_list.js', 'pastafari2')}"></script>
|
<script src="${make_media_url('js/jsutils/ajax_list.js', 'pastafari2')}"></script>
|
||||||
|
<script language="Javascript" src="${make_media_url('js/jsutils/popup.js', 'pastafari2')}"></script>
|
||||||
<script>
|
<script>
|
||||||
options={'url': "${url_for('.pastafari2_get_server_users', server_id=server_id)}"};
|
options={'url': "${url_for('.pastafari2_get_server_users', server_id=server_id)}", extra_data: {}, after_list: function () {
|
||||||
|
|
||||||
|
$('.change_password').popUp('#popup_change', 800, function (popup, container) {
|
||||||
|
|
||||||
|
$('#username_label').html($(container).html());
|
||||||
|
$('#user_form').val($(container).html());
|
||||||
|
|
||||||
|
}, undefined, {});
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
alist=$('#table_users').ajaxList('table_list', options);
|
alist=$('#table_users').ajaxList('table_list', options);
|
||||||
|
|
||||||
|
options_post={'url': '${url_for("admin_app.pastafari2_change_user_password", server_id=server_id)}', 'loading': '#layer_loading', 'success': function (data) {
|
||||||
|
|
||||||
|
if(!data.error) {
|
||||||
|
|
||||||
|
$('.close_popup').click();
|
||||||
|
|
||||||
|
//location.reload();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$('#form_change_password').sendPost(options_post);
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</%block>
|
</%block>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue