package com.jeecms.cms.action.admin.main;
import static com.jeecms.common.page.SimplePage.cpn;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jeecms.cms.entity.main.CmsGroup;
import com.jeecms.cms.entity.main.CmsRole;
import com.jeecms.cms.entity.main.CmsSite;
import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.cms.entity.main.CmsUserExt;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.common.web.RequestUtils;
/**
* 全站管理员ACTION
*
* @author liufang
*
*/
@Controller
public class CmsAdminGlobalAct extends CmsAdminAbstract {
private static final Logger log = LoggerFactory
.getLogger(CmsAdminGlobalAct.class);
@RequestMapping("/admin_global/v_list.do")
public String list(String queryUsername, String queryEmail,
Integer queryGroupId, Boolean queryDisabled, Integer pageNo,
HttpServletRequest request, ModelMap model) {
CmsUser currUser = CmsUtils.getUser(request);
Pagination pagination = manager.getPage(queryUsername, queryEmail,
null, queryGroupId, queryDisabled, true, currUser.getRank(),
cpn(pageNo), CookieUtils.getPageSize(request));
model.addAttribute("pagination", pagination);
model.addAttribute("queryUsername", queryUsername);
model.addAttribute("queryEmail", queryEmail);
model.addAttribute("queryGroupId", queryGroupId);
model.addAttribute("queryDisabled", queryDisabled);
return "admin/global/list";
}
@RequestMapping("/admin_global/v_add.do")
public String add(HttpServletRequest request, ModelMap model) {
CmsUser currUser = CmsUtils.getUser(request);
List<CmsGroup> groupList = cmsGroupMng.getList();
List<CmsSite> siteList = cmsSiteMng.getList();
List<CmsRole> roleList = cmsRoleMng.getList();
model.addAttribute("groupList", groupList);
model.addAttribute("siteList", siteList);
model.addAttribute("roleList", roleList);
model.addAttribute("currRank", currUser.getRank());
return "admin/global/add";
}
@RequestMapping("/admin_global/v_edit.do")
public String edit(Integer id, Integer queryGroupId, Boolean queryDisabled,
HttpServletRequest request, ModelMap model) {
String queryUsername = RequestUtils.getQueryParam(request,
"queryUsername");
String queryEmail = RequestUtils.getQueryParam(request, "queryEmail");
CmsUser currUser = CmsUtils.getUser(request);
WebErrors errors = validateEdit(id, request);
if (errors.hasErrors()) {
return errors.showErrorPage(model);
}
CmsUser admin = manager.findById(id);
List<CmsGroup> groupList = cmsGroupMng.getList();
List<CmsSite> siteList = cmsSiteMng.getList();
List<CmsRole> roleList = cmsRoleMng.getList();
model.addAttribute("cmsAdmin", admin);
model.addAttribute("siteIds", admin.getSiteIds());
model.addAttribute("roleIds", admin.getRoleIds());
model.addAttribute("groupList", groupList);
model.addAttribute("siteList", siteList);
model.addAttribute("roleList", roleList);
model.addAttribute("currRank", currUser.getRank());
model.addAttribute("queryUsername", queryUsername);
model.addAttribute("queryEmail", queryEmail);
model.addAttribute("queryGroupId", queryGroupId);
model.addAttribute("queryDisabled", queryDisabled);
return "admin/global/edit";
}
@RequestMapping("/admin_global/o_save.do")
public String save(CmsUser bean, CmsUserExt ext, String username,
String email, String password, Boolean viewonlyAdmin,
Boolean selfAdmin, Integer rank, Integer groupId,
Integer[] roleIds, Integer[] channelIds, Integer[] siteIds,
Byte[] steps, Boolean[] allChannels, HttpServletRequest request,
ModelMap model) {
WebErrors errors = validateSave(bean, siteIds, steps, allChannels,
request);
if (errors.hasErrors()) {
return errors.showErrorPage(model);
}
String ip = RequestUtils.getIpAddr(request);
bean = manager.saveAdmin(username, email, password, ip, viewonlyAdmin,
selfAdmin, rank, groupId, roleIds, channelIds, siteIds, steps,
allChannels, ext);
log.info("save CmsAdmin id={}", bean.getId());
cmsLogMng.operating(request, "cmsUser.log.save", "id=" + bean.getId()
+ ";username=" + bean.getUsername());
return "redirect:v_list.do";
}
@RequestMapping("/admin_global/o_update.do")
public String update(CmsUser bean, CmsUserExt ext, String password,
Integer groupId, Integer[] roleIds, Integer[] channelIds,
Integer[] siteIds, Byte[] steps, Boolean[] allChannels,
String queryUsername, String queryEmail, Integer queryGroupId,
Boolean queryDisabled, Integer pageNo, HttpServletRequest request,
ModelMap model) {
WebErrors errors = validateUpdate(bean.getId(), request);
if (errors.hasErrors()) {
return errors.showErrorPage(model);
}
bean = manager.updateAdmin(bean, ext, password, groupId, roleIds,
channelIds, siteIds, steps, allChannels);
log.info("update CmsAdmin id={}.", bean.getId());
cmsLogMng.operating(request, "cmsUser.log.update", "id=" + bean.getId()
+ ";username=" + bean.getUsername());
return list(queryUsername, queryEmail, queryGroupId, queryDisabled,
pageNo, request, model);
}
@RequestMapping("/admin_global/o_delete.do")
public String delete(Integer[] ids, Integer queryGroupId,
Boolean queryDisabled, Integer pageNo, HttpServletRequest request,
ModelMap model) {
String queryUsername = RequestUtils.getQueryParam(request,
"queryUsername");
String queryEmail = RequestUtils.getQueryParam(request, "queryEmail");
WebErrors errors = validateDelete(ids, request);
if (errors.hasErrors()) {
return errors.showErrorPage(model);
}
CmsUser[] beans = manager.deleteByIds(ids);
for (CmsUser bean : beans) {
log.info("delete CmsAdmin id={}", bean.getId());
cmsLogMng.operating(request, "cmsUser.log.delete", "id="
+ bean.getId() + ";username=" + bean.getUsername());
}
return list(queryUsername, queryEmail, queryGroupId, queryDisabled,
pageNo, request, model);
}
@RequestMapping(value = "/admin_global/v_channels_add.do")
public String channelsAdd(Integer siteId, HttpServletRequest request,
HttpServletResponse response, ModelMap model) {
return channelsAddJson(siteId, request, response, model);
}
@RequestMapping(value = "/admin_global/v_channels_edit.do")
public String channelsEdit(Integer userId, Integer siteId,
HttpServletRequest request, HttpServletResponse response,
ModelMap model) {
return channelsEditJson(userId, siteId, request, response, model);
}
@RequestMapping(value = "/admin_global/v_check_username.do")
public void checkUsername(String username, HttpServletResponse response) {
checkUserJson(username, response);
}
@RequestMapping(value = "/admin_global/v_check_email.do")
public void checkEmail(String email, HttpServletResponse response) {
checkEmailJson(email, response);
}
private WebErrors validateSave(CmsUser bean, Integer[] siteIds,
Byte[] steps, Boolean[] allChannels, HttpServletRequest request) {
WebErrors errors = WebErrors.create(request);
if (siteIds != null) {
if (steps == null) {
errors.addError("steps cannot be null");
return errors;
}
if (allChannels == null) {
errors.addError("allChannels cannot be null");
return errors;
}
if (siteIds.length != steps.length
|| siteIds.length != allChannels.length) {
errors.addError("siteIds length, steps length,"
+ " allChannels length not equals");
return errors;
}
}
return errors;
}
private WebErrors validateEdit(Integer id, HttpServletRequest request) {
WebErrors errors = WebErrors.create(request);
if (vldExist(id, errors)) {
return errors;
}
// TODO 检查管理员rank
return errors;
}
private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
WebErrors errors = WebErrors.create(request);
if (vldExist(id, errors)) {
return errors;
}
// TODO 检查管理员rank
if (vldParams(id, request, errors)) {
return errors;
}
return errors;
}
private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
WebErrors errors = WebErrors.create(request);
errors.ifEmpty(ids, "ids");
for (Integer id : ids) {
vldExist(id, errors);
}
return errors;
}
private boolean vldExist(Integer id, WebErrors errors) {
if (errors.ifNull(id, "id")) {
return true;
}
CmsUser entity = manager.findById(id);
if (errors.ifNotExist(entity, CmsUser.class, id)) {
return true;
}
return false;
}
private boolean vldParams(Integer id, HttpServletRequest request,
WebErrors errors) {
CmsUser entity = manager.findById(id);
CmsUser user = CmsUtils.getUser(request);
if (entity.getRank() > user.getRank()) {
errors.addErrorCode("error.noPermission", CmsUser.class, id);
return true;
}
return false;
}
}