mirror of https://github.com/jumpserver/jumpserver
user group quickedit frontend and rest API
parent
7f09b486d9
commit
04151b9957
|
@ -153,23 +153,29 @@ function getIDall() {
|
||||||
return check_array.join(",");
|
return check_array.join(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
function APIUpdateAttr(url, body, success, error, method) {
|
function APIUpdateAttr(props) {
|
||||||
|
// props = {url: .., body: , success: , error: , method: ,}
|
||||||
|
props = props || {};
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: url,
|
url: props.url,
|
||||||
type: method || "PATCH",
|
type: props.method || "PATCH",
|
||||||
data: body
|
data: props.body,
|
||||||
|
contentType: props.content_type || "application/json; charset=utf-8",
|
||||||
|
dataType: props.data_type || "json",
|
||||||
}).done(function(data, textStatue, jqXHR) {
|
}).done(function(data, textStatue, jqXHR) {
|
||||||
if (typeof success === 'function') {
|
if (typeof props.success === 'function') {
|
||||||
return success(data)
|
return props.success(data)
|
||||||
} else {
|
} else {
|
||||||
toastr.success('Update Success!')
|
toastr.success('Update Success!')
|
||||||
}
|
}
|
||||||
}).fail(function(jqXHR, textStatue, errorThrown) {
|
}).fail(function(jqXHR, textStatue, errorThrown) {
|
||||||
if (typeof error === 'function') {
|
if (typeof props.error === 'function') {
|
||||||
return error(errorThrown)
|
return props.error(errorThrown)
|
||||||
} else {
|
} else {
|
||||||
toastr.error('Error occurred while updating.')
|
toastr.error('Error occurred while updating.')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var jumpserver = new Object();
|
||||||
|
|
|
@ -181,31 +181,31 @@
|
||||||
<i class="fa fa-info-circle"></i> {% trans 'User group' %}
|
<i class="fa fa-info-circle"></i> {% trans 'User group' %}
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-body">
|
<div class="panel-body">
|
||||||
<table class="table">
|
<table class="table group_edit">
|
||||||
<tbody>
|
<tbody>
|
||||||
<form>
|
<form>
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="2" class="no-borders">
|
<td colspan="2" class="no-borders">
|
||||||
<select data-placeholder="{% trans 'Join user groups' %}" class="select2" style="width: 100%" multiple="" tabindex="4">
|
<select data-placeholder="{% trans 'Join user groups' %}" id="slct_groups" class="select2" style="width: 100%" multiple="" tabindex="4">
|
||||||
{% for group in groups %}
|
{% for group in groups %}
|
||||||
<option value="{{ group.id }}">{{ group.name }}</option>
|
<option value="{{ group.id }}" id="opt_{{ group.id }}">{{ group.name }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="2" class="no-borders">
|
<td colspan="2" class="no-borders">
|
||||||
<button type="button" class="btn btn-info btn-small">{% trans 'Join' %}</button>
|
<button type="button" class="btn btn-info btn-small" id="btn_add_user_group">{% trans 'Join' %}</button>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% for group in user_object.groups.all %}
|
{% for group in user_object.groups.all %}
|
||||||
<tr>
|
<tr>
|
||||||
<td ><b>{{ group.name }}</b></td>
|
<td ><b class="bdg_user_group" data-gid={{ group.id }}>{{ group.name }}</b></td>
|
||||||
<td>
|
<td>
|
||||||
<button class="btn btn-danger btn-sm" type="button" style="float: right;"><i class="fa fa-minus"></i></button>
|
<button class="btn btn-danger btn-sm btn_delete_user_group" type="button" style="float: right;"><i class="fa fa-minus"></i></button>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
@ -223,8 +223,43 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block custom_foot_js %}
|
{% block custom_foot_js %}
|
||||||
<script>
|
<script>
|
||||||
|
jumpserver.selected_groups = new Object();
|
||||||
|
function updateUserGroups(user_groups) {
|
||||||
|
var the_url = "{% url 'users:user-group-edit-api' pk=user_object.id%}";
|
||||||
|
var body = {
|
||||||
|
id: {{ user_object.id }},
|
||||||
|
groups: Object.assign([], user_groups)
|
||||||
|
};
|
||||||
|
var success = function(data) {
|
||||||
|
// remove all the selected groups from select > option and rendered ul element;
|
||||||
|
$('.select2-selection__rendered').empty();
|
||||||
|
$('#slct_groups').val('');
|
||||||
|
$.map(jumpserver.selected_groups, function(group_name, index){
|
||||||
|
$('#opt_' + index).remove();
|
||||||
|
// change tr html of user groups.
|
||||||
|
$('.group_edit tbody').append(
|
||||||
|
'<tr>' +
|
||||||
|
'<td><b class="bdg_user_group" data-gid="' + index + '">' + group_name + '</b></td>' +
|
||||||
|
'<td><button class="btn btn-danger btn-sm btn_delete_user_group" type="button" style="float: right;"><i class="fa fa-minus"></i></button></td>' +
|
||||||
|
'</tr>'
|
||||||
|
)
|
||||||
|
});
|
||||||
|
// clear jumpserver.selected_groups
|
||||||
|
jumpserver.selected_groups = {};
|
||||||
|
toastr.success('{% trans "Update success!" %}')
|
||||||
|
}
|
||||||
|
APIUpdateAttr({ url: the_url, body: JSON.stringify(body), success: success, method: 'PUT'});
|
||||||
|
|
||||||
|
}
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
$('.select2').select2();
|
$('.select2').select2()
|
||||||
|
.on('select2:select', function(evt, params) {
|
||||||
|
var data = evt.params.data;
|
||||||
|
jumpserver.selected_groups[data.id] = data.text;
|
||||||
|
}).on('select2:unselect', function(evt) {
|
||||||
|
var data = evt.params.data;
|
||||||
|
delete jumpserver.selected_groups[data.id]
|
||||||
|
})
|
||||||
});
|
});
|
||||||
$(document).on('click', '#is_active', function(){
|
$(document).on('click', '#is_active', function(){
|
||||||
var the_url = "{% url 'users:user-patch-api' pk=user_object.id %}";
|
var the_url = "{% url 'users:user-patch-api' pk=user_object.id %}";
|
||||||
|
@ -233,7 +268,7 @@ $(document).on('click', '#is_active', function(){
|
||||||
var success = function(data) {
|
var success = function(data) {
|
||||||
toastr.success('{% trans "Update success!" %}')
|
toastr.success('{% trans "Update success!" %}')
|
||||||
}
|
}
|
||||||
APIUpdateAttr(the_url, body, success);
|
APIUpdateAttr({ url: the_url, body: body, success: success});
|
||||||
}).on('click', '#enable_otp', function(){
|
}).on('click', '#enable_otp', function(){
|
||||||
var the_url = "{% url 'users:user-patch-api' pk=user_object.id %}";
|
var the_url = "{% url 'users:user-patch-api' pk=user_object.id %}";
|
||||||
var checked = !$(this).prop('checked');
|
var checked = !$(this).prop('checked');
|
||||||
|
@ -241,7 +276,33 @@ $(document).on('click', '#is_active', function(){
|
||||||
var success = function(data) {
|
var success = function(data) {
|
||||||
toastr.success('{% trans "Update success!" %}')
|
toastr.success('{% trans "Update success!" %}')
|
||||||
}
|
}
|
||||||
APIUpdateAttr(the_url, body, success);
|
APIUpdateAttr({ url: the_url, body: body, success: success});
|
||||||
});
|
}).on('click', '#btn_add_user_group', function(){
|
||||||
|
if (Object.keys(jumpserver.selected_groups).length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var user_groups = $('.bdg_user_group').map(function(){
|
||||||
|
return $(this).data('gid');
|
||||||
|
}).get();
|
||||||
|
$.map(jumpserver.selected_groups, function(value, index){
|
||||||
|
user_groups.push(parseInt(index));
|
||||||
|
$('#opt_' + index).remove();
|
||||||
|
});
|
||||||
|
updateUserGroups(user_groups)
|
||||||
|
}).on('click', '.btn_delete_user_group', function(){
|
||||||
|
var $this = $(this);
|
||||||
|
var $tr = $this.closest('tr');
|
||||||
|
var $badge = $tr.find('.bdg_user_group');
|
||||||
|
var gid = $badge.data('gid');
|
||||||
|
var group_name = $badge.html() || $badge.text();
|
||||||
|
$('#slct_groups').append(
|
||||||
|
'<option value="' + gid + '" id="opt_' + gid + '">' + group_name + '</option>'
|
||||||
|
);
|
||||||
|
$tr.remove();
|
||||||
|
var user_groups = $('.bdg_user_group').map(function(){
|
||||||
|
return $(this).data('gid');
|
||||||
|
}).get();
|
||||||
|
updateUserGroups(user_groups)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in New Issue