AppController.java 3.7 KB
Newer Older
ibizdev's avatar
ibizdev committed
1 2
package cn.ibizlab.util.rest;

3 4
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.service.IBZConfigService;
ibizdev's avatar
ibizdev committed
5
import com.alibaba.fastjson.JSONObject;
6 7 8 9
import cn.ibizlab.util.security.AuthenticationUser;
import cn.ibizlab.util.service.AuthenticationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
ibizdev's avatar
ibizdev committed
10 11
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
12
import org.springframework.security.core.GrantedAuthority;
13 14
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
15
import java.util.*;
ibizdev's avatar
ibizdev committed
16 17 18 19 20 21 22 23

@RestController
@RequestMapping(value = "")
public class AppController {

	@Value("${ibiz.enablePermissionValid:false}")
    boolean enablePermissionValid;  //是否开启权限校验

24 25 26 27
    @Value("${ibiz.systemid:ibzdisk}")
	private String systemId;


28 29 30
	@Autowired
	private AuthenticationUserService userDetailsService;

ibizdev's avatar
ibizdev committed
31 32 33 34
	@RequestMapping(method = RequestMethod.GET, value = "/appdata")
	public ResponseEntity<JSONObject> getAppData() {

		JSONObject appData = new JSONObject() ;
35 36 37
		Set<String> appMenu = new HashSet();
		Set<String> uniRes = new HashSet();

38
		AuthenticationUser curUser = AuthenticationUser.getAuthenticationUser();
ibizdev's avatar
ibizdev committed
39
		if(enablePermissionValid){
40
			Collection<GrantedAuthority> authorities=curUser.getAuthorities();
41 42 43 44
				Iterator it = authorities.iterator();
				while(it.hasNext()) {
					GrantedAuthority authority = (GrantedAuthority)it.next();
					String strAuthority=authority.getAuthority();
45 46 47 48
					if(strAuthority.startsWith("UNIRES_"+systemId))
						uniRes.add(strAuthority.substring(systemId.length()+8));
					else if(strAuthority.startsWith("APPMENU_"+systemId))
						appMenu.add(strAuthority.substring(systemId.length()+9));
49
				}
ibizdev's avatar
ibizdev committed
50
		}
51 52 53 54
		Map<String,Object> context = new HashMap<>();
		context.putAll(curUser.getSessionParams());
		context.put("srfusername",curUser.getPersonname());
		appData.put("context",context);
ibizdev's avatar
ibizdev committed
55
		appData.put("unires",uniRes);
56
    	appData.put("appmenu",appMenu);
ibizdev's avatar
ibizdev committed
57
		appData.put("enablepermissionvalid",enablePermissionValid);
58 59 60 61
		if(curUser.getSuperuser()==1)
			appData.put("enablepermissionvalid",false);
		else
			appData.put("enablepermissionvalid",enablePermissionValid);
ibizdev's avatar
ibizdev committed
62 63
		return ResponseEntity.status(HttpStatus.OK).body(appData);
	}
64 65 66 67 68 69 70

    @RequestMapping(method = RequestMethod.GET, value = "${ibiz.auth.logoutpath:v7/logout}")
    public void logout() {
		if(AuthenticationUser.getAuthenticationUser()!=null){
			userDetailsService.resetByUsername(AuthenticationUser.getAuthenticationUser().getUsername());
    	}
    }
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    @Autowired
	private IBZConfigService ibzConfigService;

	@RequestMapping(method = RequestMethod.PUT, value = "/configs/{configType}/{targetType}")
	public ResponseEntity<Boolean> saveConfig(@PathVariable("configType") String configType, @PathVariable("targetType") String targetType, @RequestBody JSONObject config) {
		String userId=AuthenticationUser.getAuthenticationUser().getUserid();
		if(StringUtils.isEmpty(userId))
			throw new BadRequestAlertException("保存配置失败,参数缺失","IBZConfig",configType);
		return ResponseEntity.ok(ibzConfigService.saveConfig(configType,targetType,userId,config));
	}

	@RequestMapping(method = RequestMethod.GET, value = "/configs/{configType}/{targetType}")
	public ResponseEntity<JSONObject> getConfig(@PathVariable("configType") String configType, @PathVariable("targetType") String targetType) {
		String userId=AuthenticationUser.getAuthenticationUser().getUserid();
		if(StringUtils.isEmpty(userId))
			throw new BadRequestAlertException("获取配置失败,参数缺失","IBZConfig",configType);
		return ResponseEntity.ok(ibzConfigService.getConfig(configType,targetType,userId));
	}
ibizdev's avatar
ibizdev committed
90
}