JdbcUsersService.java 9.1 KB
Newer Older
M
MaxKey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

package org.maxkey.synchronizer.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
M
MaxKey 已提交
24
import java.util.ArrayList;
M
MaxKey 已提交
25

M
MaxKey 已提交
26
import org.apache.commons.beanutils.PropertyUtils;
M
MaxKey 已提交
27
import org.maxkey.constants.ConstsStatus;
M
MaxKey 已提交
28
import org.maxkey.entity.DbTableMetaData;
M
MaxKey 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
import org.maxkey.entity.UserInfo;
import org.maxkey.synchronizer.AbstractSynchronizerService;
import org.maxkey.synchronizer.ISynchronizerService;
import org.maxkey.util.JdbcUtils;
import org.maxkey.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class JdbcUsersService extends AbstractSynchronizerService    implements ISynchronizerService{
	final static Logger _logger = LoggerFactory.getLogger(JdbcUsersService.class);

M
MaxKey 已提交
42 43
	static ArrayList< ColumnFieldMapper> mapperList = new ArrayList< ColumnFieldMapper>();
	
M
MaxKey 已提交
44 45
	public void sync() {
		_logger.info("Sync Jdbc Users...");
M
MaxKey 已提交
46 47 48 49
		Connection conn = null;
		Statement  stmt = null;
		ResultSet  rs 	 = null;
		
M
MaxKey 已提交
50
		try {
M
MaxKey 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
			if(StringUtils.isNotBlank(synchronizer.getOrgFilters())){
				_logger.info("Sync User Filters {}",synchronizer.getOrgFilters());
				conn = JdbcUtils.connect(
						synchronizer.getProviderUrl(), 
						synchronizer.getPrincipal(), 
						synchronizer.getCredentials(), 
						synchronizer.getDriverClass());
				
				stmt = conn.createStatement();
				rs = stmt.executeQuery(synchronizer.getUserFilters());
				long insertCount = 0;
				long updateCount = 0;
				long readCount 	 = 0;
				while(rs.next()) {
M
MaxKey 已提交
65
					UserInfo user = buildUserInfo(rs);
M
MaxKey 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
					UserInfo queryUser = this.userInfoService.findByUsername(user.getUsername());
					readCount ++;
					if(queryUser == null) {
						if(user.getPassword().indexOf("{") > -1 && user.getPassword().indexOf("}") > -1) {
							userInfoService.insert(user,false);
						}else {
							//passwordEncoder
							userInfoService.insert(user,true);
						}
						user.setBadPasswordCount(1);
						insertCount++;
					}else{
						//no need update password , set null
						user.setPassword(null);
						userInfoService.update(user);
						updateCount++;
M
MaxKey 已提交
82
					}
M
MaxKey 已提交
83
					_logger.trace("read Count {} , insert Count {} , updateCount {} " , readCount,insertCount,updateCount);
M
MaxKey 已提交
84
				}
M
MaxKey 已提交
85
				_logger.info("read Count {} , insert Count {} , updateCount {} " , readCount,insertCount,updateCount);
M
MaxKey 已提交
86 87 88
			}
		} catch (Exception e) {
			_logger.error("Exception " , e);
M
MaxKey 已提交
89 90
		}finally {
			JdbcUtils.release(conn, stmt, rs);
M
MaxKey 已提交
91 92 93 94
		}
	}
	
	public UserInfo buildUserInfo(ResultSet  rs) throws SQLException {
M
MaxKey 已提交
95
		DbTableMetaData meta = JdbcUtils.getMetaData(rs);
M
MaxKey 已提交
96
		UserInfo user = new UserInfo();
M
MaxKey 已提交
97
		//basic
M
MaxKey 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
		for (ColumnFieldMapper mapper :mapperList ) {
			if(meta.getColumnsMap().containsKey(mapper.getColumn())) {
				Object value = null;
				if(mapper.getType().equalsIgnoreCase("String")) {
					value = rs.getString(mapper.getColumn());
				}else {
					value = rs.getInt(mapper.getColumn());
				}
				if(value != null ) {
					try {
						PropertyUtils.setSimpleProperty(user, mapper.getField(), value);
					} catch (Exception e) {
						_logger.error("setSimpleProperty {}" , e);
					}
				}
			}
M
MaxKey 已提交
114
		}
M
MaxKey 已提交
115
		
M
MaxKey 已提交
116 117 118 119 120
		if(meta.getColumnsMap().containsKey("status")) {
			user.setStatus(rs.getInt("status"));
		}else {
			user.setStatus(ConstsStatus.ACTIVE);
		}
M
MaxKey 已提交
121 122
		user.setInstId(this.synchronizer.getInstId());
		
M
MaxKey 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		//password
		if(meta.getColumnsMap().containsKey("password")) {
			user.setPassword(rs.getString("password"));
		}else {
			//后4位
			String last4Char = "6666";
			if(StringUtils.isNotBlank(user.getIdCardNo())) {
				last4Char = user.getIdCardNo().substring(user.getIdCardNo().length() - 4);
			}else if(StringUtils.isNotBlank(user.getMobile())) {
				last4Char = user.getMobile().substring(user.getMobile().length() - 4);
			}else if(StringUtils.isNotBlank(user.getEmployeeNumber())) {
				last4Char = user.getEmployeeNumber().substring(user.getEmployeeNumber().length() - 4);
			}
			user.setPassword(user.getUsername()+"@M"+last4Char);
		}
M
MaxKey 已提交
138
		
M
MaxKey 已提交
139
		_logger.debug("User {} " , user);
M
MaxKey 已提交
140 141
		return user;
	}
M
MaxKey 已提交
142 143 144
	
	static {
		mapperList.add(new  ColumnFieldMapper("id"						, "id","String"));
Z
zen 已提交
145
		mapperList.add(new  ColumnFieldMapper("username"				, "userName","String"));
M
MaxKey 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		mapperList.add(new  ColumnFieldMapper("picture"					, "picture","String"));
		mapperList.add(new  ColumnFieldMapper("displayname"				, "displayName","String"));
		mapperList.add(new  ColumnFieldMapper("nickname"				, "nickName","String"));
		mapperList.add(new  ColumnFieldMapper("mobile"					, "mobile","String"));
		mapperList.add(new  ColumnFieldMapper("email"					, "email","String"));
		mapperList.add(new  ColumnFieldMapper("birthdate"				, "birthDate","String"));
		mapperList.add(new  ColumnFieldMapper("usertype"				, "userType","String"));
		mapperList.add(new  ColumnFieldMapper("userstate"				, "userState","String"));
		mapperList.add(new  ColumnFieldMapper("windowsaccount"			, "windowsAccount","String"));
		mapperList.add(new  ColumnFieldMapper("givenname"				, "givenName","String"));
		mapperList.add(new  ColumnFieldMapper("middlename"				, "middleName","String"));
		mapperList.add(new  ColumnFieldMapper("married"					, "married","Int"));
		mapperList.add(new  ColumnFieldMapper("gender"					, "gender","Int"));
		mapperList.add(new  ColumnFieldMapper("idtype"					, "idType","Int"));
		mapperList.add(new  ColumnFieldMapper("idcardno"				, "idCardNo","String"));
		mapperList.add(new  ColumnFieldMapper("website"					, "webSite","String"));
		mapperList.add(new  ColumnFieldMapper("startworkdate"			, "startWorkDate","String"));
		//work	
		mapperList.add(new  ColumnFieldMapper("workcountry"				, "workCountry","String"));
		mapperList.add(new  ColumnFieldMapper("workregion"				, "workRegion","String"));
		mapperList.add(new  ColumnFieldMapper("worklocality"			, "workLocality","String"));
		mapperList.add(new  ColumnFieldMapper("workstreetaddress"		, "workStreetAddress","String"));
		mapperList.add(new  ColumnFieldMapper("workaddressformatted"	, "workAddressFormatted","String"));
		mapperList.add(new  ColumnFieldMapper("workemail"				, "workEmail","String"));
		mapperList.add(new  ColumnFieldMapper("workphonenumber"			, "workPhoneNumber","String"));
		mapperList.add(new  ColumnFieldMapper("workpostalcode"			, "workPostalCode","String"));
		mapperList.add(new  ColumnFieldMapper("workfax"					, "workFax","String"));
		mapperList.add(new  ColumnFieldMapper("workofficename"			, "workOfficeName","String"));
		//home	
		mapperList.add(new  ColumnFieldMapper("homecountry"				, "homeCountry","String"));
		mapperList.add(new  ColumnFieldMapper("homeregion"				, "homeRegion","String"));
		mapperList.add(new  ColumnFieldMapper("homelocality"			, "homeLocality","String"));
		mapperList.add(new  ColumnFieldMapper("homestreetaddress"		, "homeStreetAddress","String"));
		mapperList.add(new  ColumnFieldMapper("homeaddressformatted"	, "homeAddressFormatted","String"));
		mapperList.add(new  ColumnFieldMapper("homeemail"				, "homeEmail","String"));
		mapperList.add(new  ColumnFieldMapper("homephonenumber"			, "homePhonenumber","String"));
		mapperList.add(new  ColumnFieldMapper("homepostalcode"			, "homePostalCode","String"));
		mapperList.add(new  ColumnFieldMapper("homefax"					, "homeFax","String"));
		//company	
		mapperList.add(new  ColumnFieldMapper("employeenumber"			, "employeeNumber","String"));
		mapperList.add(new  ColumnFieldMapper("costcenter"				, "costCenter","String"));
		mapperList.add(new  ColumnFieldMapper("organization"			, "organization","String"));
		mapperList.add(new  ColumnFieldMapper("division"				, "division","String"));
		mapperList.add(new  ColumnFieldMapper("departmentid"			, "departmentId","String"));
		mapperList.add(new  ColumnFieldMapper("department"				, "department","String"));
		mapperList.add(new  ColumnFieldMapper("jobtitle"				, "jobTitle","String"));
		mapperList.add(new  ColumnFieldMapper("joblevel"				, "jobLevel","String"));
		mapperList.add(new  ColumnFieldMapper("managerid"				, "managerId","String"));
		mapperList.add(new  ColumnFieldMapper("manager"					, "manager","String"));
		mapperList.add(new  ColumnFieldMapper("assistantid"				, "assistantId","String"));
		mapperList.add(new  ColumnFieldMapper("assistant"				, "assistant","String"));
		mapperList.add(new  ColumnFieldMapper("entrydate"				, "entrydate","String"));
		mapperList.add(new  ColumnFieldMapper("quitdate"				, "quitdate","String"));
		mapperList.add(new  ColumnFieldMapper("ldapdn"					, "ldapDn","String"));
			
		mapperList.add(new  ColumnFieldMapper("description"				, "description","String"));
		mapperList.add(new  ColumnFieldMapper("status"					, "status","String"));
	}
M
MaxKey 已提交
204
}