AppController.java.ftl 4.0 KB
Newer Older
ibizdev's avatar
ibizdev committed
1 2 3 4 5
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
package ${pub.getPKGCodeName()}.util.rest;

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

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

zhouweidong's avatar
zhouweidong committed
24 25 26
	@Value("${r'${ibiz.enablePermissionValid:false}'}")
    boolean enablePermissionValid;  //是否开启权限校验

27
    @Value("${r'${'}ibiz.systemid:${sys.getName()}}")
28 29 30
	private String systemId;


zhouweidong's avatar
zhouweidong committed
31 32 33
	@Autowired
	private AuthenticationUserService userDetailsService;

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

ibizdev's avatar
ibizdev committed
37
		JSONObject appData = new JSONObject() ;
38 39 40
		Set<String> appMenu = new HashSet();
		Set<String> uniRes = new HashSet();

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

    @RequestMapping(method = RequestMethod.GET, value = "${r'${ibiz.auth.logoutpath:v7/logout}'}")
    public void logout() {
zhouweidong's avatar
zhouweidong committed
75 76
		if(AuthenticationUser.getAuthenticationUser()!=null){
			userDetailsService.resetByUsername(AuthenticationUser.getAuthenticationUser().getUsername());
zhouweidong's avatar
zhouweidong committed
77 78
    	}
    }
sq3536's avatar
sq3536 committed
79 80 81 82 83 84 85

    @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();
zhouweidong's avatar
zhouweidong committed
86
		if(StringUtils.isEmpty(userId)){
sq3536's avatar
sq3536 committed
87
			throw new BadRequestAlertException("保存配置失败,参数缺失","IBZConfig",configType);
zhouweidong's avatar
zhouweidong committed
88
		}
sq3536's avatar
sq3536 committed
89 90 91 92 93 94
		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();
zhouweidong's avatar
zhouweidong committed
95
		if(StringUtils.isEmpty(userId)){
sq3536's avatar
sq3536 committed
96
			throw new BadRequestAlertException("获取配置失败,参数缺失","IBZConfig",configType);
zhouweidong's avatar
zhouweidong committed
97
		}
sq3536's avatar
sq3536 committed
98 99
		return ResponseEntity.ok(ibzConfigService.getConfig(configType,targetType,userId));
	}
100 101 102 103 104 105 106 107

	/**
	* 应用参数扩展
	* @param appData
	*/
	protected void fillAppData(JSONObject appData){

	}
ibizdev's avatar
ibizdev committed
108
}