-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pingan-custom] ldap_sync optimize guset/cron/add_del_mobfile
- Loading branch information
1 parent
e0e8c04
commit 4fd582e
Showing
5 changed files
with
169 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,9 @@ | |
seafile_api, ccnet_api | ||
from .ldap_conn import LdapConn | ||
from .ldap_sync import LdapSync | ||
from .utils import bytes2str | ||
from .utils import bytes2str, del_mobfile, add_mobfile | ||
from ldap import SCOPE_SUBTREE | ||
from datetime import datetime | ||
|
||
def default_ldap_role_mapping(role): | ||
return role | ||
|
@@ -375,7 +376,6 @@ def get_data_from_db(self): | |
dept = user_attrs.get('dept', '') | ||
uid = user_attrs.get('uid', '') | ||
cemail = user_attrs.get('email', '') | ||
|
||
user_data_db[user.email] = LdapUser(user.id, user.password, name, dept, | ||
uid, cemail, | ||
1 if user.is_staff else 0, | ||
|
@@ -397,18 +397,28 @@ def get_uid_to_ldap_user(self, data_ldap): | |
|
||
return uid_to_ldap_user | ||
|
||
def get_data_from_ldap_by_server(self, config): | ||
def get_data_from_ldap_by_server(self, config, use_custom_user_filter=False): | ||
if not config.enable_user_sync: | ||
return {} | ||
ldap_conn = LdapConn(config.host, config.user_dn, config.passwd, config.follow_referrals) | ||
ldap_conn.create_conn() | ||
if not ldap_conn.conn: | ||
return None | ||
|
||
# dn <-> LdapUser | ||
user_data_ldap = {} | ||
|
||
if use_custom_user_filter: | ||
|
||
if config.custom_user_filter: | ||
search_filter = '(&(objectClass=%s)(%s))' % \ | ||
(config.user_object_class, | ||
config.custom_user_filter) | ||
else: | ||
return None | ||
|
||
|
||
# search all users on base dn | ||
if config.user_filter != '': | ||
elif config.user_filter != '': | ||
search_filter = '(&(objectClass=%s)(%s))' % \ | ||
(config.user_object_class, | ||
config.user_filter) | ||
|
@@ -431,7 +441,7 @@ def get_data_from_ldap_by_server(self, config): | |
|
||
def get_data_by_base_dn(self, config, ldap_conn, base_dn, search_filter): | ||
user_data_ldap = {} | ||
search_attr = [config.login_attr, config.pwd_change_attr] | ||
search_attr = [config.login_attr, config.pwd_change_attr, config.employee_type_attr] | ||
|
||
if config.role_name_attr: | ||
search_attr.append(config.role_name_attr) | ||
|
@@ -461,12 +471,23 @@ def get_data_by_base_dn(self, config, ldap_conn, base_dn, search_filter): | |
if not isinstance(attrs, dict): | ||
continue | ||
if config.login_attr not in attrs: | ||
continue | ||
# ldap user 没有login_attr 属性,会忽略,这个 login_attr 是一个email | ||
attrs[config.login_attr] = [''] | ||
# continue | ||
if config.pwd_change_attr not in attrs: | ||
password = '' | ||
else: | ||
password = attrs[config.pwd_change_attr][0] | ||
|
||
############################################### pingan custom: 判断 ad中的emplyee type, 如果属于特殊类型,或者黑名单则不予同步 ########################### | ||
# 特殊类型从ccnet.conf中进行配置 | ||
# 在[LDAP_SYNC]中增加配置 SPECIAL_EMPLOYEE_TYPE=GM,OPR,TEST,MON,SYS,PRA,ADM | ||
if config.employee_type_attr in attrs: | ||
employee_type = attrs[config.empemployee_type_attr][0] | ||
if employee_type in config.special_account_type: | ||
continue | ||
############################################### pingan custom: 判断 ad中的emplyee type, 如果属于特殊类型,或者黑名单则不予同步 ########################### | ||
|
||
user_name = None | ||
dept = None | ||
uid = None | ||
|
@@ -505,13 +526,13 @@ def get_data_by_base_dn(self, config, ldap_conn, base_dn, search_filter): | |
else: | ||
uid = attrs[config.uid_attr][0] | ||
|
||
if config.cemail_attr != '': | ||
if config.cemail_attr not in attrs: | ||
cemail = '' | ||
else: | ||
cemail = attrs[config.cemail_attr][0] | ||
|
||
############################################### pingan custom: AD中没有邮箱,创建虚拟邮箱进行同步 ########################### | ||
email = attrs[config.login_attr][0].lower() | ||
if not email: | ||
email = "%[email protected]" % uid | ||
email = email.lower() | ||
############################################### pingan custom: AD中没有邮箱,创建虚拟邮箱进行同步 ########################### | ||
|
||
user_name = None if user_name is None else user_name.strip() | ||
user_data_ldap[email] = LdapUser(None, password, user_name, dept, | ||
uid, cemail, role = role) | ||
|
@@ -526,18 +547,30 @@ def sync_add_user(self, ldap_user, email): | |
return | ||
self.auser += 1 | ||
logger.debug('Add user [%s] success.' % email) | ||
|
||
ret = 0 | ||
if ldap_user.role: | ||
role = role_mapping(ldap_user.role) | ||
ret = ccnet_api.update_role_emailuser(email, role, False) | ||
|
||
if ret == 0: | ||
self.arole += 1 | ||
logger.debug('Add role [%s] for user [%s] success.' % (role, email)) | ||
|
||
if ret < 0: | ||
logger.warning('Add role [%s] for user [%s] failed.' % (role, email)) | ||
|
||
############################################### pingan custom: 新增用户维护访客角色, 加组 ########################### | ||
role = 'guest' | ||
ret = ccnet_api.update_role_emailuser(email, role, False) | ||
if ret == 0: | ||
self.arole += 1 | ||
logger.debug('Add role [%s] for user [%s] success.' % (role, email)) | ||
if ret < 0: | ||
logger.warning('Add role [%s] for user [%s] failed.' % (role, email)) | ||
|
||
self.add_mobfile(ldap_user) | ||
############################################### pingan custom:新增用户维护访客角色, 加组 ########################### | ||
|
||
# ret = 0 | ||
# if ldap_user.role: | ||
# role = role_mapping(ldap_user.role) | ||
# ret = ccnet_api.update_role_emailuser(email, role, False) | ||
# | ||
# if ret == 0: | ||
# self.arole += 1 | ||
# logger.debug('Add role [%s] for user [%s] success.' % (role, email)) | ||
# | ||
# if ret < 0: | ||
# logger.warning('Add role [%s] for user [%s] failed.' % (role, email)) | ||
|
||
if ldap_user.config.enable_extra_user_info_sync: | ||
self.add_profile(email, ldap_user) | ||
|
@@ -908,3 +941,33 @@ def sync_data(self, data_db, data_ldap): | |
self.update_profile_user_login_id(k, uid) | ||
|
||
self.close_seahub_db() | ||
|
||
########################### pingan custom: 1. 组的增减接口, 2. 定时同步 ##################################### | ||
|
||
def add_mobfile(self, ldap_user): | ||
op_num = 'xxxxx' | ||
flag = 0 | ||
uid = ldap_user.uid | ||
config = ldap_user.config | ||
user_dn = config.user_dn | ||
password = config.passwd | ||
add_mobfile(flag, [uid], op_num, user_dn, password) | ||
return | ||
|
||
def del_mobfile(self, ldap_user): | ||
op_num = 'xxxxxx' | ||
uid = ldap_user.uid | ||
config = ldap_user.config | ||
user_dn = config.user_dn | ||
password = config.passwd | ||
del_mobfile([uid], op_num, user_dn, password) | ||
|
||
def cron_ldap_data(self, data_ldap): | ||
current_hour = datetime.today().hour | ||
if current_hour != 6: | ||
return | ||
for email, ldap_user_obj in data_ldap.items(): | ||
self.del_mobfile(ldap_user_obj) | ||
|
||
########################### pingan custom: 1. 组的增减接口, 2. 定时同步 ##################################### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ mock | |
pytest==3.1 | ||
pyjwt | ||
pymysql | ||
requests-ntlm |