AppController.java.ftl 3.9 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();
48 49 50 51
					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));
52
				}
zhouweidong's avatar
zhouweidong committed
53
		}
54 55 56 57
		Map<String,Object> context = new HashMap<>();
		context.putAll(curUser.getSessionParams());
		context.put("srfusername",curUser.getPersonname());
		appData.put("context",context);
zhouweidong's avatar
zhouweidong committed
58
		appData.put("unires",uniRes);
zhouweidong's avatar
zhouweidong committed
59
    	appData.put("appmenu",appMenu);
zhouweidong's avatar
zhouweidong committed
60
		appData.put("enablepermissionvalid",enablePermissionValid);
zhouweidong's avatar
zhouweidong committed
61 62 63 64
		if(curUser.getSuperuser()==1)
			appData.put("enablepermissionvalid",false);
		else
			appData.put("enablepermissionvalid",enablePermissionValid);
ibizdev's avatar
ibizdev committed
65 66
		return ResponseEntity.status(HttpStatus.OK).body(appData);
	}
zhouweidong's avatar
zhouweidong committed
67 68 69

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

    @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
93
}