You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
5.8 KiB
182 lines
5.8 KiB
5 years ago
|
/*
|
||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||
|
* contributor license agreements. See the NOTICE file distributed with
|
||
|
* this work for additional information regarding copyright ownership.
|
||
|
* The ASF licenses this file to You 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.
|
||
|
*/
|
||
5 years ago
|
package org.apache.dolphinscheduler.api.service;
|
||
5 years ago
|
|
||
5 years ago
|
import org.apache.dolphinscheduler.api.enums.Status;
|
||
|
import org.apache.dolphinscheduler.api.utils.Constants;
|
||
|
import org.apache.dolphinscheduler.api.utils.PageInfo;
|
||
|
import org.apache.dolphinscheduler.common.enums.UserType;
|
||
|
import org.apache.dolphinscheduler.dao.entity.AccessToken;
|
||
|
import org.apache.dolphinscheduler.dao.entity.User;
|
||
5 years ago
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
5 years ago
|
import org.apache.dolphinscheduler.common.utils.DateUtils;
|
||
|
import org.apache.dolphinscheduler.common.utils.EncryptionUtils;
|
||
|
import org.apache.dolphinscheduler.dao.mapper.AccessTokenMapper;
|
||
5 years ago
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.stereotype.Service;
|
||
|
|
||
|
import java.util.*;
|
||
|
|
||
|
/**
|
||
|
* user service
|
||
|
*/
|
||
|
@Service
|
||
|
public class AccessTokenService extends BaseService {
|
||
|
|
||
|
private static final Logger logger = LoggerFactory.getLogger(AccessTokenService.class);
|
||
|
|
||
|
@Autowired
|
||
|
private AccessTokenMapper accessTokenMapper;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* query access token list
|
||
|
*
|
||
|
* @param loginUser
|
||
|
* @param searchVal
|
||
|
* @param pageNo
|
||
|
* @param pageSize
|
||
|
* @return
|
||
|
*/
|
||
|
public Map<String, Object> queryAccessTokenList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) {
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
|
|
||
|
PageInfo<AccessToken> pageInfo = new PageInfo<>(pageNo, pageSize);
|
||
5 years ago
|
Page<AccessToken> page = new Page(pageNo, pageSize);
|
||
|
int userId = loginUser.getId();
|
||
5 years ago
|
if (loginUser.getUserType() == UserType.ADMIN_USER){
|
||
5 years ago
|
userId = 0;
|
||
5 years ago
|
}
|
||
5 years ago
|
IPage<AccessToken> accessTokenList = accessTokenMapper.selectAccessTokenPage(page, searchVal, userId);
|
||
|
pageInfo.setTotalCount((int)accessTokenList.getTotal());
|
||
|
pageInfo.setLists(accessTokenList.getRecords());
|
||
5 years ago
|
result.put(Constants.DATA_LIST, pageInfo);
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* check
|
||
|
*
|
||
|
* @param result
|
||
|
* @param bool
|
||
|
* @param userNoOperationPerm
|
||
|
* @param status
|
||
|
* @return
|
||
|
*/
|
||
|
private boolean check(Map<String, Object> result, boolean bool, Status userNoOperationPerm, String status) {
|
||
|
//only admin can operate
|
||
|
if (bool) {
|
||
|
result.put(Constants.STATUS, userNoOperationPerm);
|
||
|
result.put(status, userNoOperationPerm.getMsg());
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* create token
|
||
|
*
|
||
|
* @param userId
|
||
|
* @param expireTime
|
||
|
* @param token
|
||
|
* @return
|
||
|
*/
|
||
|
public Map<String, Object> createToken(int userId, String expireTime, String token) {
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
|
|
||
|
AccessToken accessToken = new AccessToken();
|
||
|
accessToken.setUserId(userId);
|
||
|
accessToken.setExpireTime(DateUtils.stringToDate(expireTime));
|
||
|
accessToken.setToken(token);
|
||
5 years ago
|
accessToken.setCreateTime(new Date());
|
||
|
accessToken.setUpdateTime(new Date());
|
||
5 years ago
|
|
||
|
// insert
|
||
|
int insert = accessTokenMapper.insert(accessToken);
|
||
|
|
||
|
if (insert > 0) {
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
} else {
|
||
|
putMsg(result, Status.CREATE_ALERT_GROUP_ERROR);
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* generate token
|
||
|
* @param userId
|
||
|
* @param expireTime
|
||
|
* @return
|
||
|
*/
|
||
|
public Map<String, Object> generateToken(int userId, String expireTime) {
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
|
String token = EncryptionUtils.getMd5(userId + expireTime + String.valueOf(System.currentTimeMillis()));
|
||
|
result.put(Constants.DATA_LIST, token);
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
* delete access token
|
||
|
* @param loginUser
|
||
|
* @param id
|
||
|
* @return
|
||
|
*/
|
||
5 years ago
|
public Map<String, Object> delAccessTokenById(User loginUser, int id) {
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
|
//only admin can operate
|
||
|
if (!isAdmin(loginUser)) {
|
||
|
putMsg(result, Status.USER_NOT_EXIST, id);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
5 years ago
|
accessTokenMapper.deleteById(id);
|
||
5 years ago
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
5 years ago
|
|
||
|
/**
|
||
|
* update token by id
|
||
|
* @param id
|
||
|
* @param userId
|
||
|
* @param expireTime
|
||
|
* @param token
|
||
|
* @return
|
||
|
*/
|
||
|
public Map<String, Object> updateToken(int id,int userId, String expireTime, String token) {
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
|
AccessToken accessToken = new AccessToken();
|
||
|
accessToken.setId(id);
|
||
|
accessToken.setUserId(userId);
|
||
|
accessToken.setExpireTime(DateUtils.stringToDate(expireTime));
|
||
|
accessToken.setToken(token);
|
||
|
accessToken.setUpdateTime(new Date());
|
||
|
|
||
5 years ago
|
accessTokenMapper.updateById(accessToken);
|
||
5 years ago
|
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
5 years ago
|
}
|