diff --git a/seahub/api2/endpoints/account.py b/seahub/api2/endpoints/account.py index 191f626da8..c33a40b504 100644 --- a/seahub/api2/endpoints/account.py +++ b/seahub/api2/endpoints/account.py @@ -30,6 +30,7 @@ json_content_type = 'application/json; charset=utf-8' def get_account_info(user): email = user.username d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) + profile = Profile.objects.get_profile_by_user(email) info = {} info['email'] = email @@ -39,6 +40,7 @@ def get_account_info(user): info['is_staff'] = user.is_staff info['is_active'] = user.is_active info['create_time'] = user.ctime + info['login_id'] = profile.login_id if profile else '' info['total'] = seafile_api.get_user_quota(email) info['usage'] = seafile_api.get_user_self_usage(email) @@ -108,10 +110,18 @@ class Account(APIView): profile = Profile.objects.get_profile_by_user(email) if profile is None: profile = Profile(user=email) - profile.nickname = name profile.save() + # update account loginid + loginid = request.data.get("login_id", '').strip() + if loginid != '': + profile = Profile.objects.get_profile_by_user(email) + if profile is None: + profile = Profile(user=email) + profile.login_id = loginid + profile.save() + # update account detailed profile department = request.data.get("department", None) if department is not None: @@ -165,6 +175,18 @@ class Account(APIView): return api_error(status.HTTP_400_BAD_REQUEST, _(u"Name should not include '/'.")) + #argument check for loginid + loginid = request.data.get("login_id", None) + if loginid is not None: + loginid = loginid.strip() + if loginid == "": + return api_error(status.HTTP_400_BAD_REQUEST, + _(u"Login id can't be empty")) + usernamebyloginid = Profile.objects.get_username_by_login_id(loginid) + if usernamebyloginid is not None: + return api_error(status.HTTP_400_BAD_REQUEST, + _(u"Login id %s already exists." % loginid)) + # argument check for department department = request.data.get("department", None) if department is not None: diff --git a/seahub/templates/sysadmin/userinfo.html b/seahub/templates/sysadmin/userinfo.html index 416a19d343..997e3e3826 100644 --- a/seahub/templates/sysadmin/userinfo.html +++ b/seahub/templates/sysadmin/userinfo.html @@ -53,6 +53,18 @@ +
{% trans "Login ID" %}
+
+ + {% if profile and profile.login_id %} + {{ profile.login_id }} + {% else %} + -- + {% endif %} + + +
+
{% trans "Department" %}
@@ -98,6 +110,13 @@ +
{% csrf_token %} +

{% trans "Set user Login ID" %}

+
+

+ +
+
{% csrf_token %}

{% trans "Set user storage limit" %}

MB @@ -300,6 +319,10 @@ $('#set-name').click(function() { $("#set-name-form").modal({appendTo: "#main"}); $('#simplemodal-container').css({'width':'auto', 'height':'auto'}); }); +$('#set-loginid').click(function () { + $("#set-loginid-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'}); @@ -351,6 +374,43 @@ $('#set-name-form').submit(function() { return false; }); +$('#set-loginid-form').submit(function() { + var loginid = $.trim($('[name="loginid"]', $(this)).val()); + var $loginid = $('#loginid'); + var $error = $('.error', $(this)); + var $submitBtn = $('[type="submit"]', $(this)); + if (!loginid){ + $error.html("{% trans "Login id can't be empty" %}").show(); + return false; + } + disable($submitBtn); + + $.ajax({ + url: '{% url 'api2-account' email %}', + type: 'PUT', + dataType: 'json', + cache: false, + beforeSend: prepareCSRFToken, + data: {'login_id': loginid}, + success: function(data) { + $loginid.html(HTMLescape(data['login_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'); diff --git a/tests/api/endpoints/test_account.py b/tests/api/endpoints/test_account.py index 1adb35b828..7365ffd216 100644 --- a/tests/api/endpoints/test_account.py +++ b/tests/api/endpoints/test_account.py @@ -45,7 +45,40 @@ class AccountTest(BaseTestCase): def _do_update(self): return self.client.put( reverse('api2-account', args=[self.user1.username]), - 'password=654321&is_staff=1&is_active=0&name=user1&storage=102400', + 'password=654321&is_staff=1&is_active=0&name=user1&storage=102400&login_id=hello', + 'application/x-www-form-urlencoded', + ) + + def _do_update_name(self): + return self.client.put( + reverse('api2-account', args=[self.user1.username]), + 'name=user1', + 'application/x-www-form-urlencoded', + ) + + def _do_update_loginid(self): + return self.client.put( + reverse('api2-account', args=[self.user1.username]), + 'login_id=hello', + 'application/x-www-form-urlencoded', + ) + + def _do_update_loginid_useemptystring(self): + return self.client.put( + reverse('api2-account', args=[self.user1.username]), + 'login_id=', + 'application/x-www-form-urlencoded', + ) + + def _do_update_loginid_sendagain(self): + self.client.put( + reverse('api2-account', args=[self.user1.username]), + 'login_id=test', + 'application/x-www-form-urlencoded', + ) + return self.client.put( + reverse('api2-account', args=[self.user1.username]), + 'login_id=test', 'application/x-www-form-urlencoded', ) @@ -77,7 +110,7 @@ class AccountTest(BaseTestCase): resp = self._do_get_info() json_resp = json.loads(resp.content) - assert len(json_resp) == 9 + assert len(json_resp) == 10 assert json_resp['email'] == self.user1.username assert json_resp['is_staff'] is False assert json_resp['is_active'] is True @@ -99,11 +132,39 @@ class AccountTest(BaseTestCase): '654321')) self.assertTrue(User.objects.get(self.user1.username).is_staff) self.assertFalse(User.objects.get(self.user1.username).is_active) + self.assertEqual(Profile.objects.get_profile_by_user( + self.user1.username).login_id, 'hello') self.assertEqual(Profile.objects.get_profile_by_user( self.user1.username).nickname, 'user1') self.assertEqual(seafile_api.get_user_quota( self.user1.username), 102400000000) + def test_update_name(self): + """only test name""" + self.login_as(self.admin) + resp = self._do_update_name() + self.assertEqual(Profile.objects.get_profile_by_user( + self.user1.username).nickname, 'user1') + + def test_update_loginid(self): + """only test loginid""" + self.login_as(self.admin) + resp = self._do_update_loginid() + self.assertEqual(Profile.objects.get_profile_by_user( + self.user1.username).login_id, 'hello') + + def test_update_loginid_useemptystring(self): + """test loginid, longid send the empty""" + self.login_as(self.admin) + resp = self._do_update_loginid_useemptystring() + self.assertEqual(400, resp.status_code) + + def test_update_loginid_sendagain(self): + """test loginid,sent twice""" + self.login_as(self.admin) + resp = self._do_update_loginid_sendagain() + self.assertEqual(400, resp.status_code) + def test_refresh_profile_cache_after_update(self): self.login_as(self.admin) self.assertEqual(email2nickname(self.user1.username),