123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /*
- * Copyright [2020] [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.persistence.service;
- import java.util.List;
- import org.apache.mybatis.jpa.persistence.JpaBaseService;
- import org.maxkey.constants.ConstsStatus;
- import org.maxkey.crypto.password.PasswordReciprocal;
- import org.maxkey.entity.Accounts;
- import org.maxkey.entity.AccountsStrategy;
- import org.maxkey.entity.OrganizationsCast;
- import org.maxkey.entity.UserInfo;
- import org.maxkey.persistence.mapper.AccountsMapper;
- import org.maxkey.provision.ProvisionService;
- import org.maxkey.provision.ProvisionAction;
- import org.maxkey.provision.ProvisionTopic;
- import org.maxkey.util.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import net.sourceforge.pinyin4j.PinyinHelper;
- import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
- import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
- import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
- import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
- import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
- @Repository
- public class AccountsService extends JpaBaseService<Accounts>{
- @Autowired
- ProvisionService mqPersistService;
-
- @Autowired
- UserInfoService userInfoService;
-
- @Autowired
- AccountsStrategyService accountsStrategyService;
-
- @Autowired
- OrganizationsCastService organizationsCastService;
-
- public AccountsService() {
- super(AccountsMapper.class);
- }
- /* (non-Javadoc)
- * @see com.connsec.db.service.BaseService#getMapper()
- */
- @Override
- public AccountsMapper getMapper() {
- return (AccountsMapper)super.getMapper();
- }
-
-
- public boolean insert(Accounts account) {
- if (super.insert(account)) {
- if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
- UserInfo loadUserInfo = userInfoService.findUserRelated(account.getUserId());
- account.setUserInfo(loadUserInfo);
- OrganizationsCast cast = new OrganizationsCast();
- cast.setProvider(account.getAppId());
- cast.setOrgId(loadUserInfo.getDepartmentId());
- account.setOrgCast(organizationsCastService.query(cast));
- mqPersistService.send(
- ProvisionTopic.ACCOUNT_TOPIC,
- account,
- ProvisionAction.CREATE_ACTION);
- }
-
- return true;
- }
- return false;
- }
-
- public boolean update(Accounts account) {
- if (super.update(account)) {
- if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
- UserInfo loadUserInfo = userInfoService.findUserRelated(account.getUserId());
- account.setUserInfo(loadUserInfo);
- OrganizationsCast cast = new OrganizationsCast();
- cast.setProvider(account.getAppId());
- cast.setOrgId(loadUserInfo.getDepartmentId());
- account.setOrgCast(organizationsCastService.query(cast));
- mqPersistService.send(
- ProvisionTopic.ACCOUNT_TOPIC,
- account,
- ProvisionAction.UPDATE_ACTION);
- }
-
- return true;
- }
- return false;
- }
-
- public boolean updateStatus(Accounts accounts) {
- return this.getMapper().updateStatus(accounts) > 0;
- }
- public boolean remove(String id) {
- Accounts account = this.get(id);
- if (super.remove(id)) {
- UserInfo loadUserInfo = null;
- if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
- loadUserInfo = userInfoService.findUserRelated(account.getUserId());
- account.setUserInfo(loadUserInfo);
- mqPersistService.send(
- ProvisionTopic.ACCOUNT_TOPIC,
- account,
- ProvisionAction.DELETE_ACTION);
- }
-
- return true;
- }
- return false;
- }
-
- public void refreshByStrategy(AccountsStrategy strategy) {
- if(StringUtils.isNotBlank(strategy.getOrgIdsList())) {
- strategy.setOrgIdsList("'"+strategy.getOrgIdsList().replace(",", "','")+"'");
- }
- List<UserInfo> userList = queryUserNotInStrategy(strategy);
- for(UserInfo user : userList) {
- Accounts account = new Accounts();
- account.setAppId(strategy.getAppId());
- account.setAppName(strategy.getAppName());
-
- account.setUserId(user.getId());
- account.setUsername(user.getUsername());
- account.setDisplayName(user.getDisplayName());
- account.setRelatedUsername(generateAccount(user,strategy));
- account.setRelatedPassword(PasswordReciprocal.getInstance().encode(userInfoService.randomPassword()));
-
- account.setInstId(strategy.getInstId());
- account.setCreateType("automatic");
- account.setStatus(ConstsStatus.ACTIVE);
- account.setStrategyId(strategy.getId());
-
- insert(account);
- }
- deleteByStrategy(strategy);
- }
- public void refreshAllByStrategy() {
- AccountsStrategy queryStrategy = new AccountsStrategy();
- queryStrategy.setCreateType("automatic");
- for( AccountsStrategy strategy : accountsStrategyService.query(queryStrategy)) {
- refreshByStrategy(strategy);
- }
- }
-
-
- public List<UserInfo> queryUserNotInStrategy(AccountsStrategy strategy){
- return getMapper().queryUserNotInStrategy(strategy);
- }
-
- public long deleteByStrategy(AccountsStrategy strategy) {
- return getMapper().deleteByStrategy(strategy);
- }
-
-
- public List<Accounts> queryByAppIdAndDate(Accounts account) {
- return getMapper().queryByAppIdAndDate(account);
- }
-
- public List<Accounts> queryByAppIdAndAccount(String appId,String relatedUsername){
- return getMapper().queryByAppIdAndAccount(appId,relatedUsername);
- }
-
-
- public String generateAccount(UserInfo userInfo,AccountsStrategy accountsStrategy) {
- String shortAccount = generateAccount(userInfo,accountsStrategy,true);
- String account = generateAccount(userInfo,accountsStrategy,false);
- String accountResult = shortAccount;
- List<Accounts> AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),shortAccount +accountsStrategy.getSuffixes());
- if(!AccountsList.isEmpty()) {
- if(accountsStrategy.getMapping().equalsIgnoreCase("email")) {
- accountResult = account;
- AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),account + accountsStrategy.getSuffixes());
- }
- if(!AccountsList.isEmpty()) {
- for(int i =1 ;i < 100 ;i++) {
- accountResult = account + i;
- AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),accountResult + accountsStrategy.getSuffixes());
- if(AccountsList.isEmpty())break;
- }
- }
- }
- if(StringUtils.isNotBlank(accountsStrategy.getSuffixes())){
- accountResult = accountResult + accountsStrategy.getSuffixes();
- }
- return accountResult;
- }
-
-
- private String generateAccount(UserInfo userInfo,AccountsStrategy strategy,boolean isShort) {
- String account = "";
- if(strategy.getMapping().equalsIgnoreCase("username")) {
- account = userInfo.getUsername();
- }else if(strategy.getMapping().equalsIgnoreCase("mobile")) {
- account = userInfo.getMobile();
- }else if(strategy.getMapping().equalsIgnoreCase("email")) {
- try {
- if(isShort) {
- account = getPinYinShortName(userInfo.getDisplayName());
- }else {
- account = getPinYinName(userInfo.getDisplayName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- }
- }else if(strategy.getMapping().equalsIgnoreCase("employeeNumber")) {
- account = userInfo.getEmployeeNumber();
- }else if(strategy.getMapping().equalsIgnoreCase("windowsAccount")) {
- account = userInfo.getWindowsAccount();
- }else if(strategy.getMapping().equalsIgnoreCase("idCardNo")) {
- account = userInfo.getIdCardNo();
- }else {
- account = userInfo.getUsername();
- }
-
- return account;
- }
-
- public static String getPinYinName(String name) throws BadHanyuPinyinOutputFormatCombination {
- HanyuPinyinOutputFormat pinyinFormat = new HanyuPinyinOutputFormat();
- pinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- pinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- pinyinFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
- return PinyinHelper.toHanYuPinyinString(name, pinyinFormat, "",false);
- }
-
- public static String getPinYinShortName(String name) throws BadHanyuPinyinOutputFormatCombination {
- char[] strs = name.toCharArray();
- String pinyinName = "";
- for(int i=0;i<strs.length;i++) {
- if(i == 0) {
- pinyinName += getPinYinName(strs[i]+"");
- }else {
- pinyinName += getPinYinName(strs[i]+"").charAt(0);
- }
- }
- return pinyinName;
- }
-
- }
|