AppListController.java 11.2 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */
 

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
18 19
package org.maxkey.web.contorller;

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
20
import java.security.Principal;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
21
import java.util.List;
M
Notices  
MaxKey 已提交
22 23

import org.maxkey.configuration.ApplicationConfig;
M
v 3.3.0  
MaxKey 已提交
24 25 26
import org.maxkey.constants.ConstsOperateMessage;
import org.maxkey.constants.ConstsProtocols;
import org.maxkey.constants.ConstsStatus;
M
MaxKey 已提交
27
import org.maxkey.crypto.password.PasswordReciprocal;
M
MaxKey 已提交
28 29 30 31
import org.maxkey.entity.Accounts;
import org.maxkey.entity.UserInfo;
import org.maxkey.entity.apps.Apps;
import org.maxkey.entity.apps.UserApps;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
32 33 34
import org.maxkey.persistence.service.AccountsService;
import org.maxkey.persistence.service.AppsService;
import org.maxkey.persistence.service.UserInfoService;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
import org.maxkey.web.WebContext;
import org.maxkey.web.message.Message;
import org.maxkey.web.message.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
48 49 50 51 52 53
/**
 * AppListController.
 * 
 * @author Administrator
 *
 */
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
54
@Controller
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
55 56 57
public class AppListController {
    static final Logger _logger = LoggerFactory.getLogger(AppListController.class);

M
Notices  
MaxKey 已提交
58 59 60
    @Autowired
  	protected ApplicationConfig applicationConfig;
    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    AccountsService appUsersService;

    @Autowired
    AppsService appsService;

    /**
     * gridList.
     * @param gridList 类型
     * @return
     */
    @RequestMapping(value = { "/appList" })
    public ModelAndView appList(
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
77
            @RequestParam(value = "gridList", required = false) String gridList,Principal principal) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
78
        ModelAndView modelAndView = new ModelAndView("main/appList");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
79
        userInfoService.updateGridList(gridList);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
80
        modelAndView.addObject("appList", queryAccessableApps());
M
Notices  
MaxKey 已提交
81
        modelAndView.addObject("noticesVisible", applicationConfig.isNoticesVisible());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
82 83 84 85 86 87 88
        return modelAndView;
    }

    @RequestMapping(value = { "/appConfigList" })
    public ModelAndView appConfigList() {
        ModelAndView modelAndView = new ModelAndView("main/appConfigList");
        modelAndView.addObject("appList", queryAccessableApps());
M
Notices  
MaxKey 已提交
89
        
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
90 91 92 93 94 95
        return modelAndView;
    }

    private List<UserApps> queryAccessableApps() {
        UserApps userApplications = new UserApps();
        userApplications.setUsername(WebContext.getUserInfo().getUsername());
M
MaxKey 已提交
96
        userApplications.setInstId(WebContext.getUserInfo().getInstId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
97
        List<UserApps> appList = appsService.queryMyApps(userApplications);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
98
        for (UserApps app : appList) {
M
MaxKey 已提交
99
        	app.transIconBase64();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        }

        return appList;
    }

    /**
     * forwardAppLoginConfig.
     * @param protocol protocol
     * @param credential credential
     * @param appId appId
     * @return
     */
    @RequestMapping(value = { "/forward/appProtectedConfig/{protocol}/{credential}/{appId}" })
    public ModelAndView forwardAppLoginConfig(@PathVariable("protocol") String protocol,
            @PathVariable("credential") int credential, @PathVariable("appId") String appId) {
        ModelAndView modelAndView = new ModelAndView("main/appProtectedConfig");

        UserInfo userInfo = WebContext.getUserInfo();

        if (userInfo.getProtectedAppsMap().get(appId) != null) {
            modelAndView.addObject("protectedappId", true);
        } else {
            modelAndView.addObject("protectedappId", false);
        }
M
MaxKey 已提交
124
        modelAndView.addObject("userId", userInfo.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        modelAndView.addObject("appId", appId);
        modelAndView.addObject("protocol", protocol);
        modelAndView.addObject("credential", credential);
        return modelAndView;

    }

    /**
     * appLoginConfig.
     * @param protocol protocol
     * @param credential credential
     * @param appId appId
     * @param protectedappId protectedappId
     * @param password password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = { "/appProtectedConfig" })
    public Message appLoginConfig(
                @RequestParam("protocol") String protocol, 
                @RequestParam("credential") int credential,
                @RequestParam("appId") String appId, 
                @RequestParam("protectedappId") String protectedappId,
                @RequestParam("password") String password) {

        UserInfo userInfo = WebContext.getUserInfo();
M
MaxKey 已提交
151
        String userAppProtectedPassword = PasswordReciprocal.getInstance().decoder(userInfo.getAppLoginPassword());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
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
        if (userAppProtectedPassword.equals(password)) {

            if (protectedappId.equalsIgnoreCase("YES")) {
                if (userInfo.getProtectedApps() != null 
                        && userInfo.getProtectedApps().indexOf(appId) < 0) {
                    userInfo.setProtectedApps(userInfo.getProtectedApps() + "," + appId);
                    if (userInfo.getProtectedAppsMap() != null) {
                        userInfo.getProtectedAppsMap().put(appId, appId);
                    }
                } else {
                    userInfo.setProtectedApps("," + appId);
                }
            } else {
                if (userInfo.getProtectedApps() != null 
                        && userInfo.getProtectedApps().indexOf(appId) > -1) {
                    // userInfo.setSecondProtectedApps(userInfo.getSecondProtectedApps()+","+appId);
                    String[] protectedApps = userInfo.getProtectedApps().split(",");
                    String protectedAppIds = "";
                    if (userInfo.getProtectedAppsMap() != null) {
                        userInfo.getProtectedAppsMap().remove(appId);
                    }
                    for (String protectedAppId : protectedApps) {
                        if (protectedAppId.equalsIgnoreCase(appId) 
                                || protectedAppId.trim().equals("")) {
                            continue;
                        }
                        protectedAppIds = protectedAppIds + "," + protectedAppId;
                    }
                    userInfo.setProtectedApps(protectedAppIds);
                }
            }

            userInfoService.updateProtectedApps(userInfo);
        } else {
M
v 3.3.0  
MaxKey 已提交
186
            return new Message(WebContext.getI18nValue(ConstsOperateMessage.UPDATE_ERROR), MessageType.error);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
187 188
        }

M
v 3.3.0  
MaxKey 已提交
189
        return new Message(WebContext.getI18nValue(ConstsOperateMessage.UPDATE_SUCCESS), MessageType.success);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
190 191 192 193 194 195 196 197 198 199 200 201
    }

    @RequestMapping(value = { "/forward/appUserConfig/{protocol}/{credential}/{appId}" })
    public ModelAndView forwardAppUserConfig(@PathVariable("protocol") String protocol,
            @PathVariable("credential") int credential, @PathVariable("appId") String appId) {
        ModelAndView modelAndView = new ModelAndView("main/appUserConfig");
        // modelAndView.addObject("appList",appList);

        Accounts appUsers = new Accounts();
        UserInfo userInfo = WebContext.getUserInfo();
        if (credential == Apps.CREDENTIALS.USER_DEFINED) {
            appUsers = appUsersService.load(new Accounts(userInfo.getId(), appId));
M
v 3.3.0  
MaxKey 已提交
202 203 204
            if (protocol.equalsIgnoreCase(ConstsProtocols.FORMBASED)
                    || protocol.equalsIgnoreCase(ConstsProtocols.BASIC) 
                    || protocol.equalsIgnoreCase(ConstsProtocols.EXTEND_API)) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
205 206 207

                modelAndView.addObject("username", true);
                modelAndView.addObject("password", true);
M
v 3.3.0  
MaxKey 已提交
208
            } else if (protocol.equalsIgnoreCase(ConstsProtocols.SAML20)) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
209 210 211 212 213 214 215 216
                modelAndView.addObject("username", true);
                modelAndView.addObject("password", false);
            } else {
                modelAndView.addObject("username", false);
                modelAndView.addObject("password", false);
            }
            if (appUsers != null) {
                modelAndView.addObject("identity_username", appUsers.getRelatedUsername());
M
MaxKey 已提交
217
                modelAndView.addObject("identity_password", PasswordReciprocal.getInstance().decoder(appUsers.getRelatedPassword()));
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
218 219 220 221 222 223 224 225 226
            } else {
                modelAndView.addObject("identity_username", "");
                modelAndView.addObject("identity_password", "");
            }
        } else {
            modelAndView.addObject("username", false);
            modelAndView.addObject("password", false);
        }

M
MaxKey 已提交
227
        modelAndView.addObject("userId", userInfo.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        modelAndView.addObject("appId", appId);
        modelAndView.addObject("protocol", protocol);
        modelAndView.addObject("credential", credential);
        return modelAndView;

    }

    @ResponseBody
    @RequestMapping(value = { "/appUserConfig" })
    public Message appUserConfig(@RequestParam("protocol") String protocol, @RequestParam("credential") int credential,
            @RequestParam("appId") String appId, @RequestParam("identity_username") String identity_username,
            @RequestParam("identity_password") String identity_password) {

        Apps app = appsService.get(appId);
        UserInfo userInfo = WebContext.getUserInfo();

        Accounts appUsers = new Accounts();
        appUsers.setAppId(appId);
M
MaxKey 已提交
246
        appUsers.setUserId(userInfo.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
247 248 249 250 251 252 253 254

        if (identity_password != null && !identity_password.equals("") && credential == Apps.CREDENTIALS.USER_DEFINED) {
            appUsers = appUsersService.load(new Accounts(userInfo.getId(), appId));
            if (appUsers == null) {
                appUsers = new Accounts();
                appUsers.setId(appUsers.generateId());
                appUsers.setAppId(appId);
                appUsers.setAppName(app.getName());
M
MaxKey 已提交
255
                appUsers.setUserId(userInfo.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
256 257 258 259
                appUsers.setUsername(userInfo.getUsername());
                appUsers.setDisplayName(userInfo.getDisplayName());

                appUsers.setRelatedUsername(identity_username);
M
MaxKey 已提交
260
                appUsers.setRelatedPassword(PasswordReciprocal.getInstance().encode(identity_password));
M
MaxKey 已提交
261
                appUsers.setInstId(userInfo.getInstId());
M
v 3.3.0  
MaxKey 已提交
262
                appUsers.setStatus(ConstsStatus.ACTIVE);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
263 264 265
                appUsersService.insert(appUsers);
            } else {
                appUsers.setRelatedUsername(identity_username);
M
MaxKey 已提交
266
                appUsers.setRelatedPassword(PasswordReciprocal.getInstance().encode(identity_password));
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
267 268 269 270
                appUsersService.update(appUsers);
            }
        }

M
v 3.3.0  
MaxKey 已提交
271
        return new Message(WebContext.getI18nValue(ConstsOperateMessage.UPDATE_SUCCESS), MessageType.success);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
272
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
273
}