From 57c8fa967cd38ccb8ea0f08e18bdfb1fc6f4649d Mon Sep 17 00:00:00 2001
From: lian <imwhatiam123@gmail.com>
Date: Sat, 10 Oct 2020 15:40:57 +0800
Subject: [PATCH 1/3] custom shibboleth get user role (#4689)

Co-authored-by: lian <lian@seafile.com>
---
 thirdpart/shibboleth/middleware.py | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/thirdpart/shibboleth/middleware.py b/thirdpart/shibboleth/middleware.py
index 730112248c..9309edaa11 100755
--- a/thirdpart/shibboleth/middleware.py
+++ b/thirdpart/shibboleth/middleware.py
@@ -1,6 +1,8 @@
 from collections import OrderedDict
 from fnmatch import fnmatch
 import logging
+import os
+import sys
 
 from django.conf import settings
 from django.contrib.auth.middleware import RemoteUserMiddleware
@@ -21,6 +23,13 @@ from seahub.utils.user_permissions import get_user_role
 # Get an instance of a logger
 logger = logging.getLogger(__name__)
 
+conf_dir = os.environ['SEAFILE_CENTRAL_CONF_DIR']
+sys.path.append(conf_dir)
+try:
+    from seahub_custom_functions import custom_shibboleth_get_user_role
+    CUSTOM_SHIBBOLETH_GET_USER_ROLE = True
+except ImportError as e:
+    CUSTOM_SHIBBOLETH_GET_USER_ROLE = False
 
 class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
     """
@@ -97,9 +106,19 @@ class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
             user.save()
             # call make profile.
             self.make_profile(user, shib_meta)
-            user_role = self.update_user_role(user, shib_meta)
+
+            if CUSTOM_SHIBBOLETH_GET_USER_ROLE:
+                user_role = custom_shibboleth_get_user_role(shib_meta)
+                if user_role:
+                    ccnet_api.update_role_emailuser(user.email, user_role)
+                else:
+                    user_role = self.update_user_role(user, shib_meta)
+            else:
+                user_role = self.update_user_role(user, shib_meta)
+
             if user_role:
                 self.update_user_quota(user, user_role)
+
             #setup session.
             self.setup_session(request)
             request.shib_login = True

From 62c2f0bc9ed729c34dbb8cdf33a10727fcce3d1f Mon Sep 17 00:00:00 2001
From: lian <imwhatiam123@gmail.com>
Date: Sat, 10 Oct 2020 16:05:33 +0800
Subject: [PATCH 2/3] update for dev env (#4690)

Co-authored-by: lian <lian@seafile.com>
---
 thirdpart/shibboleth/middleware.py | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/thirdpart/shibboleth/middleware.py b/thirdpart/shibboleth/middleware.py
index 9309edaa11..64af3a1405 100755
--- a/thirdpart/shibboleth/middleware.py
+++ b/thirdpart/shibboleth/middleware.py
@@ -18,19 +18,22 @@ from seahub.base.accounts import User
 from seahub.base.sudo_mode import update_sudo_mode_ts
 from seahub.profile.models import Profile
 from seahub.utils.file_size import get_quota_from_string
-from seahub.utils.user_permissions import get_user_role
 
 # Get an instance of a logger
 logger = logging.getLogger(__name__)
 
-conf_dir = os.environ['SEAFILE_CENTRAL_CONF_DIR']
-sys.path.append(conf_dir)
 try:
-    from seahub_custom_functions import custom_shibboleth_get_user_role
-    CUSTOM_SHIBBOLETH_GET_USER_ROLE = True
-except ImportError as e:
+    conf_dir = os.environ['SEAFILE_CENTRAL_CONF_DIR']
+    sys.path.append(conf_dir)
+    try:
+        from seahub_custom_functions import custom_shibboleth_get_user_role
+        CUSTOM_SHIBBOLETH_GET_USER_ROLE = True
+    except ImportError:
+        CUSTOM_SHIBBOLETH_GET_USER_ROLE = False
+except KeyError:
     CUSTOM_SHIBBOLETH_GET_USER_ROLE = False
 
+
 class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
     """
     Authentication Middleware for use with Shibboleth.  Uses the recommended pattern
@@ -52,15 +55,15 @@ class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
                 " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
                 " before the RemoteUserMiddleware class.")
 
-        #To support logout.  If this variable is True, do not
-        #authenticate user and return now.
+        # To support logout.  If this variable is True, do not
+        # authenticate user and return now.
         if request.session.get(LOGOUT_SESSION_KEY) is True:
             return
         else:
-            #Delete the shib reauth session key if present.
+            # Delete the shib reauth session key if present.
             request.session.pop(LOGOUT_SESSION_KEY, None)
 
-        #Locate the remote user header.
+        # Locate the remote user header.
         # import pprint; pprint.pprint(request.META)
         try:
             username = request.META[SHIB_USER_HEADER]
@@ -119,7 +122,7 @@ class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
             if user_role:
                 self.update_user_quota(user, user_role)
 
-            #setup session.
+            # setup session.
             self.setup_session(request)
             request.shib_login = True
 
@@ -253,5 +256,6 @@ class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
                     error = True
         return shib_attrs, error
 
+
 class ShibbolethValidationError(Exception):
     pass

From a3342df76a7a2843f3d845a16f3c42fc0f5be8d6 Mon Sep 17 00:00:00 2001
From: llj <lingjun.li1@gmail.com>
Date: Tue, 13 Oct 2020 11:45:08 +0800
Subject: [PATCH 3/3] [system admin] statistic/traffic: modified 'order_by'
 value according to python API, for sort of users/orgs traffic (#4691)

---
 .../src/pages/sys-admin/statistic/statistic-traffic-orgs.js     | 2 +-
 .../src/pages/sys-admin/statistic/statistic-traffic-users.js    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/frontend/src/pages/sys-admin/statistic/statistic-traffic-orgs.js b/frontend/src/pages/sys-admin/statistic/statistic-traffic-orgs.js
index 64204c8dbf..b155d96e82 100644
--- a/frontend/src/pages/sys-admin/statistic/statistic-traffic-orgs.js
+++ b/frontend/src/pages/sys-admin/statistic/statistic-traffic-orgs.js
@@ -74,7 +74,7 @@ class OrgsTraffic extends React.Component {
 
   getTrafficList = (month, page) => {
     const { perPage, sortBy, sortOrder } = this.state;
-    const orderBy = `${sortBy}_${sortOrder}`;
+    const orderBy = sortOrder == 'asc' ? sortBy : `${sortBy}_${sortOrder}`;
     this.setState({isLoading: true, errorMessage: ''});
     seafileAPI.sysAdminListOrgTraffic(month, page, perPage, orderBy).then(res => {
       let orgTrafficList = res.data.org_monthly_traffic_list.slice(0);
diff --git a/frontend/src/pages/sys-admin/statistic/statistic-traffic-users.js b/frontend/src/pages/sys-admin/statistic/statistic-traffic-users.js
index 8988c64887..8e8ad2cb5c 100644
--- a/frontend/src/pages/sys-admin/statistic/statistic-traffic-users.js
+++ b/frontend/src/pages/sys-admin/statistic/statistic-traffic-users.js
@@ -74,7 +74,7 @@ class UsersTraffic extends React.Component {
 
   getTrafficList = (month, page) => {
     const { perPage, sortBy, sortOrder } = this.state;
-    const orderBy = `${sortBy}_${sortOrder}`;
+    const orderBy = sortOrder == 'asc' ? sortBy : `${sortBy}_${sortOrder}`;
     this.setState({
       isLoading: true,
       errorMessage: ''