mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-18 08:16:07 +00:00
[API] add user reference id
This commit is contained in:
@@ -7,6 +7,7 @@ from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from seaserv import seafile_api, ccnet_api
|
||||
|
||||
@@ -66,6 +67,21 @@ def update_user_info(request):
|
||||
profile.nickname = name
|
||||
profile.save()
|
||||
|
||||
# update account login_id
|
||||
login_id = request.data.get("login_id", None)
|
||||
if login_id is not None:
|
||||
login_id = login_id.strip()
|
||||
profile = Profile.objects.get_profile_by_user(email)
|
||||
if profile is None:
|
||||
profile = Profile(user=email)
|
||||
profile.login_id = None if login_id == "" else login_id
|
||||
profile.save()
|
||||
|
||||
reference_id = request.data.get("reference_id", None)
|
||||
if reference_id is not None:
|
||||
reference_id = reference_id.strip()
|
||||
ccnet_api.set_reference_id(email, reference_id)
|
||||
|
||||
department = request.data.get("department")
|
||||
if department:
|
||||
d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email)
|
||||
@@ -88,15 +104,18 @@ def get_user_info(email):
|
||||
|
||||
user = User.objects.get(email=email)
|
||||
d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email)
|
||||
profile = Profile.objects.get_profile_by_user(email)
|
||||
|
||||
info = {}
|
||||
info['email'] = email
|
||||
info['name'] = email2nickname(email)
|
||||
info['contact_email'] = email2contact_email(email)
|
||||
info['login_id'] = profile.login_id if profile and profile.login_id else ''
|
||||
|
||||
info['is_staff'] = user.is_staff
|
||||
info['is_active'] = user.is_active
|
||||
info['create_time'] = user.ctime
|
||||
info['reference_id'] = user.reference_id if user.reference_id else ''
|
||||
|
||||
info['department'] = d_profile.department if d_profile else ''
|
||||
|
||||
@@ -300,6 +319,20 @@ class AdminUser(APIView):
|
||||
error_msg = "Name should not include '/'."
|
||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||
|
||||
# argument check for login_id
|
||||
login_id = request.data.get("login_id", None)
|
||||
if login_id is not None:
|
||||
login_id = login_id.strip()
|
||||
username_by_login_id = Profile.objects.get_username_by_login_id(login_id)
|
||||
if username_by_login_id is not None:
|
||||
return api_error(status.HTTP_400_BAD_REQUEST,
|
||||
_(u"Login id %s already exists." % login_id))
|
||||
|
||||
reference_id = request.data.get("reference_id", None)
|
||||
if reference_id is not None:
|
||||
if not is_valid_username(reference_id) and reference_id != "":
|
||||
return api_error(status.HTTP_400_BAD_REQUEST, 'Reference ID %s invalid.' % reference_id)
|
||||
|
||||
department = request.data.get("department", None)
|
||||
if department:
|
||||
if len(department) > 512:
|
||||
|
@@ -100,6 +100,7 @@ class UserManager(object):
|
||||
user.org = emailuser.org
|
||||
user.source = emailuser.source
|
||||
user.role = emailuser.role
|
||||
user.reference_id = emailuser.reference_id
|
||||
|
||||
return user
|
||||
|
||||
|
@@ -65,6 +65,14 @@
|
||||
<span id="set-loginid" title="{% trans "Edit"%}" class="sf2-icon-edit op-icon"></span>
|
||||
</dd>
|
||||
|
||||
<dt>{% trans "Reference ID" %}</dt>
|
||||
<dd>
|
||||
<span id="referenceID">
|
||||
{{ reference_id }}
|
||||
</span>
|
||||
<span id="set-referenceID" title="{% trans "Edit" %}" class="sf2-icon-edit op-icon"></span>
|
||||
</dd>
|
||||
|
||||
<dt>{% trans "Department" %}</dt>
|
||||
<dd>
|
||||
<span id="department">
|
||||
@@ -130,6 +138,13 @@
|
||||
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
||||
</form>
|
||||
|
||||
<form id="set-referenceID-form" method="post" action="" class="hide">{% csrf_token %}
|
||||
<h3>{% trans "Set user Reference ID" %}</h3>
|
||||
<input type="text" name="referenceID" class="input" value="" /><br />
|
||||
<p class="error hide"></p>
|
||||
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
||||
</form>
|
||||
|
||||
<form id="set-quota-form" method="post" class="hide">{% csrf_token %}
|
||||
<h3>{% trans "Set user storage limit" %}</h3>
|
||||
<input type="text" name="space_quota" class="input" /> MB
|
||||
@@ -336,6 +351,10 @@ $('#set-loginid').click(function () {
|
||||
$("#set-loginid-form").modal({appendTo:"#main"});
|
||||
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
||||
})
|
||||
$('#set-referenceID').click(function () {
|
||||
$("#set-referenceID-form").modal({appendTo:"#main"});
|
||||
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
||||
})
|
||||
$('#set-dept').click(function() {
|
||||
$("#set-dept-form").modal({appendTo: "#main"});
|
||||
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
||||
@@ -379,12 +398,12 @@ $('#set-name-form').submit(function() {
|
||||
disable($submitBtn);
|
||||
|
||||
$.ajax({
|
||||
url: '{% url 'api2-account' email %}',
|
||||
url: '{% url 'api-v2.1-admin-user' email %}',
|
||||
type: 'PUT',
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: prepareCSRFToken,
|
||||
data: {'name': nickname},
|
||||
data: {'email': '{{email}}', 'name': nickname},
|
||||
success: function(data) {
|
||||
if (nickname == '') {
|
||||
$name.html('--');
|
||||
@@ -420,12 +439,12 @@ $('#set-loginid-form').submit(function() {
|
||||
disable($submitBtn);
|
||||
|
||||
$.ajax({
|
||||
url: '{% url 'api2-account' email %}',
|
||||
url: '{% url 'api-v2.1-admin-user' email %}',
|
||||
type: 'PUT',
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: prepareCSRFToken,
|
||||
data: {'login_id': loginid},
|
||||
data: {'email': '{{email}}', 'login_id': loginid},
|
||||
success: function(data) {
|
||||
$loginid.html(HTMLescape(data['login_id']));
|
||||
$.modal.close();
|
||||
@@ -445,6 +464,43 @@ $('#set-loginid-form').submit(function() {
|
||||
return false;
|
||||
});
|
||||
|
||||
$('#set-referenceID-form').submit(function() {
|
||||
var referenceID = $.trim($('[name="referenceID"]', $(this)).val());
|
||||
var $referenceID = $('#referenceID');
|
||||
var $error = $('.error', $(this));
|
||||
var $submitBtn = $('[type="submit"]', $(this));
|
||||
if (!referenceID){
|
||||
$error.html("{% trans "Reference id can't be empty" %}").show();
|
||||
return false;
|
||||
}
|
||||
disable($submitBtn);
|
||||
|
||||
$.ajax({
|
||||
url: '{% url 'api-v2.1-admin-user' email %}',
|
||||
type: 'PUT',
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: prepareCSRFToken,
|
||||
data: {'email': '{{email}}', 'reference_id': referenceID},
|
||||
success: function(data) {
|
||||
$referenceID.html(HTMLescape(data['reference_id']));
|
||||
$.modal.close();
|
||||
},
|
||||
error: function(xhr, textStatus, errorThrown) {
|
||||
var err_msg;
|
||||
if (xhr.responseText) {
|
||||
err_msg = $.parseJSON(xhr.responseText).error_msg;
|
||||
} else {
|
||||
err_msg = "{% trans "Failed. Please check the network." %}";
|
||||
}
|
||||
$error.html(err_msg).show();
|
||||
enable($submitBtn);
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
$('#set-dept-form').submit(function() {
|
||||
var department = $.trim($('[name="department"]', $(this)).val());
|
||||
var $department = $('#department');
|
||||
@@ -453,12 +509,12 @@ $('#set-dept-form').submit(function() {
|
||||
disable($submitBtn);
|
||||
|
||||
$.ajax({
|
||||
url: '{% url 'api2-account' email %}',
|
||||
url: '{% url 'api-v2.1-admin-user' email %}',
|
||||
type: 'PUT',
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: prepareCSRFToken,
|
||||
data: {'department': department},
|
||||
data: {'email': '{{email}}', 'department': department},
|
||||
success: function(data) {
|
||||
if (department == '') {
|
||||
$department.html('--');
|
||||
@@ -497,17 +553,17 @@ $('#set-quota-form').submit(function() {
|
||||
disable($submitBtn);
|
||||
|
||||
$.ajax({
|
||||
url: '{% url 'api2-account' email %}',
|
||||
url: '{% url 'api-v2.1-admin-user' email %}',
|
||||
type: 'PUT',
|
||||
dataType: 'json',
|
||||
cache: false,
|
||||
beforeSend: prepareCSRFToken,
|
||||
data: {'storage': space_quota},
|
||||
data: {'email': '{{email}}', 'quota_total': space_quota},
|
||||
success: function(data) {
|
||||
if (space_quota == 0) {
|
||||
$quota.html('--');
|
||||
} else {
|
||||
$quota.html(quotaSizeFormat(parseInt(data['total']), 1));
|
||||
$quota.html(quotaSizeFormat(parseInt(data['quota_total']), 1));
|
||||
}
|
||||
$.modal.close();
|
||||
},
|
||||
|
@@ -698,11 +698,14 @@ def user_info(request, email):
|
||||
else:
|
||||
g.role = _('Member')
|
||||
|
||||
_user = User.objects.get(email=email)
|
||||
|
||||
reference_id = _user.reference_id
|
||||
|
||||
_default_device = False
|
||||
_has_two_factor_auth = has_two_factor_auth()
|
||||
if _has_two_factor_auth:
|
||||
from seahub.two_factor.utils import default_device
|
||||
_user = User.objects.get(email=email)
|
||||
_default_device = default_device(_user)
|
||||
|
||||
return render_to_response(
|
||||
@@ -720,6 +723,7 @@ def user_info(request, email):
|
||||
'personal_groups': personal_groups,
|
||||
'two_factor_auth_enabled': _has_two_factor_auth,
|
||||
'default_device': _default_device,
|
||||
'reference_id': reference_id if reference_id else '',
|
||||
}, context_instance=RequestContext(request))
|
||||
|
||||
@login_required_ajax
|
||||
|
@@ -288,3 +288,45 @@ class AdminUserTest(BaseTestCase):
|
||||
self.login_as(self.user)
|
||||
resp = self.client.delete(self.url)
|
||||
self.assertEqual(403, resp.status_code)
|
||||
|
||||
def test_update_login_id(self):
|
||||
self.login_as(self.admin)
|
||||
|
||||
data = {"email": self.tmp_email, "login_id": ''}
|
||||
resp = self.client.put(self.url, json.dumps(data),
|
||||
'application/json')
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['login_id'] == ''
|
||||
|
||||
data = {"email": self.tmp_email, "login_id": 'lg_id'}
|
||||
resp = self.client.put(self.url, json.dumps(data),
|
||||
'application/json')
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['login_id'] == 'lg_id'
|
||||
|
||||
data = {"email": self.tmp_email, "login_id": ''}
|
||||
resp = self.client.put(self.url, json.dumps(data),
|
||||
'application/json')
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['login_id'] == ''
|
||||
|
||||
def test_update_reference_id(self):
|
||||
self.login_as(self.admin)
|
||||
|
||||
data = {"email": self.tmp_email, "reference_id": ''}
|
||||
resp = self.client.put(self.url, json.dumps(data),
|
||||
'application/json')
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['reference_id'] == ''
|
||||
|
||||
data = {"email": self.tmp_email, "reference_id": 'rf@id.com'}
|
||||
resp = self.client.put(self.url, json.dumps(data),
|
||||
'application/json')
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['reference_id'] == 'rf@id.com'
|
||||
|
||||
data = {"email": self.tmp_email, "reference_id": ''}
|
||||
resp = self.client.put(self.url, json.dumps(data),
|
||||
'application/json')
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['reference_id'] == ''
|
||||
|
Reference in New Issue
Block a user