소스 검색

记录管理员的操作日志

yaobo 7 년 전
부모
커밋
743f9f7c84

+ 24 - 5
src/main/java/cn/efunbox/audio/controller/AdminController.java

@@ -3,7 +3,9 @@ package cn.efunbox.audio.controller;
 import cn.efunbox.audio.consts.Consts;
 import cn.efunbox.audio.consts.Status;
 import cn.efunbox.audio.entity.Admin;
+import cn.efunbox.audio.entity.Trail;
 import cn.efunbox.audio.service.AdminService;
+import cn.efunbox.audio.service.TrailService;
 import cn.efunbox.audio.utils.ApiCode;
 import cn.efunbox.audio.utils.Common;
 import cn.efunbox.audio.utils.HttpUtil;
@@ -36,6 +38,9 @@ public class AdminController {
     AdminService adminService;
     @Autowired
     StringRedisTemplate stringRedisTemplate;
+    @Autowired
+    TrailService trailService;
+
     @Value("${admin.token.expire}")
     int tokenExpire = 24*30;
 
@@ -141,11 +146,24 @@ public class AdminController {
 
     @RequestMapping(value = "/register", method = RequestMethod.POST)
     public void Register(HttpServletRequest request, HttpServletResponse response, @RequestBody Admin admin){
+
+        String id = request.getHeader("id");
+        Admin superAdmin = adminService.GetOne(Long.valueOf(id));
+        if(superAdmin==null || false==superAdmin.getName().equalsIgnoreCase("superadmin")){
+            HttpUtil.responseApiCode(request, response, ApiCode.ACCESS_DENIED);
+            return;
+        }
+        TrailController.Insert(adminService, trailService, request, "admin", admin.getId(), " 创建管理员:" + admin.getName());
         InsertUpdate(request, response, admin);
     }
 
     @RequestMapping(value = "/update", method = RequestMethod.POST)
     public void Update(HttpServletRequest request, HttpServletResponse response, @RequestBody Admin admin){
+        if(admin.getName().equalsIgnoreCase("superadmin")){
+            HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
+            return;
+        }
+        TrailController.Insert(adminService, trailService, request, "admin", admin.getId(), " 修改管理员:" + admin.getName());
         InsertUpdate(request, response, admin);
     }
 
@@ -154,22 +172,23 @@ public class AdminController {
         String idAdmin = request.getParameter("idAdmin");
         adminService.Delete(Long.valueOf(idAdmin));
         HttpUtil.responseOk(request, response);
+        TrailController.Insert(adminService, trailService, request, "admin", Long.valueOf(idAdmin), " 删除管理员:" + idAdmin);
         return;
     }
 
-    public void InsertUpdate(HttpServletRequest request, HttpServletResponse response, Admin admin){
+    public Admin InsertUpdate(HttpServletRequest request, HttpServletResponse response, Admin admin){
         String name = admin.getName();
         String pwd = admin.getPwd();
         if(admin.getId()==null){
 
             if(name==null || name.length()<4 || pwd==null || pwd.length()<4){
                 HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
-                return;
+                return admin;
             }
             List<Admin> list = adminService.SearchByName(name);
             if(list!=null && list.size()>0){
                 HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
-                return;
+                return admin;
             }
             admin.setStatus(Status.ONLINE.getCode());
             admin.setCreated(new Timestamp(new Date().getTime()));
@@ -177,7 +196,7 @@ public class AdminController {
             Admin adminOld = adminService.GetOne(admin.getId());
             if(adminOld==null){
                 HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
-                return;
+                return admin;
             }
             admin = (Admin)Common.CopyValue(admin, adminOld);
         }
@@ -194,7 +213,7 @@ public class AdminController {
         SaveRedis(admin);
 
         HttpUtil.responseOkData(request, response, admin);
-        return;
+        return admin;
     }
 
     /**

+ 11 - 0
src/main/java/cn/efunbox/audio/controller/AudioController.java

@@ -46,6 +46,10 @@ public class AudioController {
     RightsService rightsService;
     @Autowired
     AudioHttp audioHttp;
+    @Autowired
+    AdminService adminService;
+    @Autowired
+    TrailService trailService;
 
     /**
      * 管理员搜索接口
@@ -219,6 +223,8 @@ public class AudioController {
         audio.setCreated(new Timestamp(new Date().getTime()));
         audio = audioService.Insert(audio);
 
+        TrailController.Insert(adminService, trailService, request, "audio", audio.getId(), " 新建资源:" + audio.toString());
+
         HttpUtil.responseOkData(request, response, audio);
         return;
     }
@@ -239,6 +245,8 @@ public class AudioController {
         audio = (Audio)Common.CopyValue(audio, audioOld);
         audio = audioService.Insert(audio);
 
+        TrailController.Insert(adminService, trailService, request, "audio", audio.getId(), " 修改资源:" + audio.toString());
+
         HttpUtil.responseOkData(request, response, audio);
         return;
     }
@@ -248,6 +256,9 @@ public class AudioController {
         String idAudio = request.getParameter("idAudio");
         audioService.Delete(Long.valueOf(idAudio));
         HttpUtil.responseOk(request, response);
+
+        TrailController.Insert(adminService, trailService, request, "audio", Long.valueOf(idAudio), " 删除资源:" + idAudio);
+
         return;
     }
 

+ 15 - 0
src/main/java/cn/efunbox/audio/controller/ChannelController.java

@@ -2,7 +2,9 @@ package cn.efunbox.audio.controller;
 
 import cn.efunbox.audio.consts.Status;
 import cn.efunbox.audio.entity.Channel;
+import cn.efunbox.audio.service.AdminService;
 import cn.efunbox.audio.service.ChannelService;
+import cn.efunbox.audio.service.TrailService;
 import cn.efunbox.audio.utils.ApiCode;
 import cn.efunbox.audio.utils.Common;
 import cn.efunbox.audio.utils.HttpUtil;
@@ -32,6 +34,10 @@ public class ChannelController {
 
     @Autowired
     ChannelService channelService;
+    @Autowired
+    AdminService adminService;
+    @Autowired
+    TrailService trailService;
 
     @RequestMapping(value = "/search" ,method = RequestMethod.POST)
     public void Search(HttpServletRequest request, HttpServletResponse response){
@@ -111,6 +117,9 @@ public class ChannelController {
                 channelService.Insert(c);
             }
         }
+
+        TrailController.Insert(adminService, trailService, request, "channel", channel.getId(), " 新建渠道:" + channel.toString());
+
         HttpUtil.responseOkData(request, response, channel);
         return;
     }
@@ -130,6 +139,9 @@ public class ChannelController {
             channel.setHasSon(true);
         channel = channelService.Insert(channel);
 //        System.out.println("ch3:"+channel.toString());
+
+        TrailController.Insert(adminService, trailService, request, "channel", channel.getId(), " 更新渠道:" + channel.toString());
+
         HttpUtil.responseOkData(request, response, channel);
         return;
     }
@@ -154,6 +166,9 @@ public class ChannelController {
                 }
             }
         }
+
+        TrailController.Insert(adminService, trailService, request, "channel", Long.valueOf(idChannel), " 删除渠道:" + channel.toString());
+
         HttpUtil.responseOk(request, response);
         return;
     }

+ 15 - 0
src/main/java/cn/efunbox/audio/controller/GroupingController.java

@@ -3,7 +3,9 @@ package cn.efunbox.audio.controller;
 import cn.efunbox.audio.consts.Status;
 import cn.efunbox.audio.entity.Channel;
 import cn.efunbox.audio.entity.Grouping;
+import cn.efunbox.audio.service.AdminService;
 import cn.efunbox.audio.service.GroupingService;
+import cn.efunbox.audio.service.TrailService;
 import cn.efunbox.audio.utils.ApiCode;
 import cn.efunbox.audio.utils.Common;
 import cn.efunbox.audio.utils.HttpUtil;
@@ -33,6 +35,10 @@ public class GroupingController {
 
     @Autowired
     GroupingService groupingService;
+    @Autowired
+    AdminService adminService;
+    @Autowired
+    TrailService trailService;
 
     @RequestMapping(value = "/search" ,method = RequestMethod.POST)
     public void Search(HttpServletRequest request, HttpServletResponse response){
@@ -119,6 +125,9 @@ public class GroupingController {
             g.setHasSon(true);
             groupingService.Insert(g);
         }
+
+        TrailController.Insert(adminService, trailService, request, "grouping", grouping.getId(), " 新建资源分组:" + grouping.toString());
+
         HttpUtil.responseOkData(request, response, grouping);
         return;
     }
@@ -134,6 +143,9 @@ public class GroupingController {
         if(groupingOld.getHasSon())
             grouping.setHasSon(true);
         grouping = groupingService.Insert(grouping);
+
+        TrailController.Insert(adminService, trailService, request, "grouping", grouping.getId(), " 更新资源分组:" + grouping.toString());
+
         HttpUtil.responseOkData(request, response, grouping);
         return;
     }
@@ -154,6 +166,9 @@ public class GroupingController {
                 }
             }
         }
+
+        TrailController.Insert(adminService, trailService, request, "grouping", grouping.getId(), " 删除资源分组:" + grouping.toString());
+
         HttpUtil.responseOk(request, response);
         return;
     }

+ 13 - 5
src/main/java/cn/efunbox/audio/controller/RightsController.java

@@ -5,9 +5,7 @@ import cn.efunbox.audio.entity.Channel;
 import cn.efunbox.audio.entity.Grouping;
 import cn.efunbox.audio.entity.Record;
 import cn.efunbox.audio.entity.Rights;
-import cn.efunbox.audio.service.ChannelService;
-import cn.efunbox.audio.service.GroupingService;
-import cn.efunbox.audio.service.RightsService;
+import cn.efunbox.audio.service.*;
 import cn.efunbox.audio.utils.ApiCode;
 import cn.efunbox.audio.utils.Common;
 import cn.efunbox.audio.utils.HttpUtil;
@@ -37,12 +35,14 @@ public class RightsController {
 
     @Autowired
     RightsService rightsService;
-
     @Autowired
     ChannelService channelService;
-
     @Autowired
     GroupingService groupingService;
+    @Autowired
+    AdminService adminService;
+    @Autowired
+    TrailService trailService;
 
     @RequestMapping(value = "/search" ,method = RequestMethod.POST)
     public void Search(HttpServletRequest request, HttpServletResponse response){
@@ -147,6 +147,8 @@ public class RightsController {
         rights.setCreated(new Timestamp(new Date().getTime()));
         rights = rightsService.Insert(rights);
 
+        TrailController.Insert(adminService, trailService, request, "rights", rights.getId(), " 新建渠道对资源的访问权限:" + rights.toString());
+
         HttpUtil.responseOkData(request, response, rights);
         return;
     }
@@ -160,6 +162,9 @@ public class RightsController {
         }
         rights = (Rights) Common.CopyValue(rights, rightsOld);
         rights = rightsService.Insert(rights);
+
+        TrailController.Insert(adminService, trailService, request, "rights", rights.getId(), " 更新渠道对资源的访问权限:" + rights.toString());
+
         HttpUtil.responseOkData(request, response, rights);
         return;
     }
@@ -168,6 +173,9 @@ public class RightsController {
     public void Delete(HttpServletRequest request, HttpServletResponse response){
         String idRights = request.getParameter("idRights");
         rightsService.Delete(Long.valueOf(idRights));
+
+        TrailController.Insert(adminService, trailService, request, "rights", Long.valueOf(idRights), " 删除渠道对资源的访问权限:" + idRights);
+
         HttpUtil.responseOk(request, response);
         return;
     }

+ 100 - 0
src/main/java/cn/efunbox/audio/controller/TrailController.java

@@ -0,0 +1,100 @@
+package cn.efunbox.audio.controller;
+
+import cn.efunbox.audio.consts.Status;
+import cn.efunbox.audio.entity.Admin;
+import cn.efunbox.audio.entity.Channel;
+import cn.efunbox.audio.entity.Trail;
+import cn.efunbox.audio.impl.AdminServiceImpl;
+import cn.efunbox.audio.impl.TrailServiceImpl;
+import cn.efunbox.audio.service.AdminService;
+import cn.efunbox.audio.service.ChannelService;
+import cn.efunbox.audio.service.TrailService;
+import cn.efunbox.audio.utils.ApiCode;
+import cn.efunbox.audio.utils.Common;
+import cn.efunbox.audio.utils.HttpUtil;
+import com.netflix.discovery.converters.Auto;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.sql.Timestamp;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+@RestController
+@Slf4j
+@RequestMapping(value = "/trail")
+public class TrailController {
+
+    @Autowired
+    TrailService trailService;
+    @Autowired
+    AdminService adminService;
+
+    @RequestMapping(value = "/search" ,method = RequestMethod.POST)
+    public void Search(HttpServletRequest request, HttpServletResponse response){
+        String idAdmin = request.getParameter("idAdmin");
+        String target = request.getParameter("target");
+        String idTarget = request.getParameter("idTarget");
+        String page = request.getParameter("page");
+        String size = request.getParameter("size");
+        if(page==null || page.length()<1)
+            page = "0";
+        if(size==null || size.length()<1)
+            size = "0";
+        Map<String,Object> map = new HashMap<>();
+        Page<Trail> pageRes = null;
+        if(idAdmin!=null && idAdmin.length()>0)
+            pageRes = trailService.SearchByIdAdmin(Long.valueOf(idAdmin), Integer.valueOf(page), Integer.valueOf(size));
+        else if(target!=null && target.length()>0 && idTarget!=null && idTarget.length()>0)
+            pageRes = trailService.SearchByTargetAndIdTarget(target, Long.valueOf(idTarget), Integer.valueOf(page), Integer.valueOf(size));
+        else if(target!=null && target.length()>0)
+            pageRes = trailService.SearchByTarget(target, Integer.valueOf(page), Integer.valueOf(size));
+        else if(idTarget!=null && idTarget.length()>0)
+            pageRes = trailService.SearchByIdTarget(Long.valueOf(idTarget), Integer.valueOf(page), Integer.valueOf(size));
+        else
+            pageRes = trailService.SearchAll(Integer.valueOf(page), Integer.valueOf(size));
+
+        if(pageRes!=null){
+            map.put("data", pageRes.getContent());
+            map.put("size", pageRes.getNumberOfElements());
+            map.put("total", pageRes.getTotalElements());
+        }
+
+        HttpUtil.responseOkData(request, response, map);
+        return;
+    }
+
+    /**
+     * 插入新的记录,非接口形式
+     * @param request
+     * @param target
+     * @param idTarget
+     * @param desc
+     * @return
+     */
+    public static Trail Insert(AdminService adminService, TrailService trailService, HttpServletRequest request, String target, Long idTarget, String desc){
+        String id = request.getHeader("id");
+        Admin adminOld = adminService.GetOne(Long.valueOf(id));
+        Trail trail = new Trail();
+        trail.setIdAdmin(Long.valueOf(id));
+        trail.setCreated(new Timestamp(new Date().getTime()));
+        trail.setTarget(target);
+        trail.setIdTarget(idTarget);
+        trail.setText("管理员 " + adminOld.getName() + desc);
+        trail = trailService.Insert(trail);
+        return trail;
+    }
+
+}

+ 57 - 0
src/main/java/cn/efunbox/audio/entity/Trail.java

@@ -0,0 +1,57 @@
+package cn.efunbox.audio.entity;
+
+
+import lombok.Data;
+import lombok.ToString;
+import org.hibernate.annotations.DynamicInsert;
+import org.hibernate.annotations.DynamicUpdate;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+/**
+ * 管理员操作记录
+ * Created by yao on 17-9-26.
+ */
+@Data
+@ToString
+@Entity
+@DynamicInsert
+@DynamicUpdate
+public class Trail implements Serializable, Cloneable {
+
+    @Id
+    @GeneratedValue
+    private Long id;
+
+    //管理员编号
+    @Column
+    private Long idAdmin = 0L;
+
+    //操作对象,表名称
+    @Column
+    private String target;
+
+    //对象id
+    @Column
+    private Long idTarget;
+
+    //操作描述
+    @Column
+    private String text;
+
+    @Column
+//    @Temporal(TemporalType.TIMESTAMP)
+//    private Date created;
+    private Timestamp created;
+
+    public Object clone() throws CloneNotSupportedException {
+        return super.clone();
+    }
+
+
+}

+ 83 - 0
src/main/java/cn/efunbox/audio/impl/TrailServiceImpl.java

@@ -0,0 +1,83 @@
+package cn.efunbox.audio.impl;
+
+import cn.efunbox.audio.entity.Channel;
+import cn.efunbox.audio.entity.Trail;
+import cn.efunbox.audio.repository.ChannelRepo;
+import cn.efunbox.audio.repository.TrailRepo;
+import cn.efunbox.audio.service.ChannelService;
+import cn.efunbox.audio.service.TrailService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+@Service
+public class TrailServiceImpl implements TrailService {
+
+    @Autowired
+    TrailRepo trailRepo;
+
+    @Override
+    public Trail GetOne(Long id){
+        Trail trail = trailRepo.findOne(id);
+        return trail;
+    }
+
+    @Override
+    public Page<Trail> SearchAll(int page, int size){
+        if(size<=0)
+            size = 50;
+        Pageable pageable = new PageRequest(page, size);
+        Page<Trail> list = trailRepo.findAll(pageable);
+        return list;
+    }
+
+    @Override
+    public Page<Trail> SearchByIdAdmin(Long idAdmin, int page, int size){
+        if(size<=0)
+            size = 50;
+        Pageable pageable = new PageRequest(page, size);
+        Page<Trail> list = trailRepo.findByIdAdmin(idAdmin, pageable);
+        return list;
+    }
+
+    @Override
+    public Page<Trail> SearchByTarget(String target, int page, int size){
+        if(size<=0)
+            size = 50;
+        Pageable pageable = new PageRequest(page, size);
+        Page<Trail> list = trailRepo.findByTarget(target, pageable);
+        return list;
+    }
+
+    @Override
+    public Page<Trail> SearchByIdTarget(Long idTarget, int page, int size){
+        if(size<=0)
+            size = 50;
+        Pageable pageable = new PageRequest(page, size);
+        Page<Trail> list = trailRepo.findByIdTarget(idTarget, pageable);
+        return list;
+    }
+
+    @Override
+    public Page<Trail> SearchByTargetAndIdTarget(String target, Long idTarget, int page, int size){
+        if(size<=0)
+            size = 50;
+        Pageable pageable = new PageRequest(page, size);
+        Page<Trail> list = trailRepo.findByTargetAndIdTarget(target, idTarget, pageable);
+        return list;
+    }
+
+    @Override
+    public Trail Insert(Trail trail){
+        Trail t = trailRepo.save(trail);
+        return t;
+    }
+
+}

+ 25 - 0
src/main/java/cn/efunbox/audio/repository/TrailRepo.java

@@ -0,0 +1,25 @@
+package cn.efunbox.audio.repository;
+
+import cn.efunbox.audio.entity.Channel;
+import cn.efunbox.audio.entity.Trail;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+@Repository
+public interface TrailRepo extends JpaRepository<Trail, Long> {
+
+    public List<Trail> findById(Long id);
+    public Page<Trail> findAll(Pageable pageable);
+    public Page<Trail> findByIdAdmin(Long idAdmin, Pageable pageable);
+    public Page<Trail> findByTarget(String target, Pageable pageable);
+    public Page<Trail> findByIdTarget(Long idTarget, Pageable pageable);
+    public Page<Trail> findByTargetAndIdTarget(String target, Long idTarget, Pageable pageable);
+
+}

+ 27 - 0
src/main/java/cn/efunbox/audio/service/TrailService.java

@@ -0,0 +1,27 @@
+package cn.efunbox.audio.service;
+
+import cn.efunbox.audio.entity.Channel;
+import cn.efunbox.audio.entity.Trail;
+import org.springframework.data.domain.Page;
+
+import java.util.List;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+public interface TrailService {
+
+    public Trail GetOne(Long id);
+
+    public Page<Trail> SearchAll(int page, int size);
+
+    public Page<Trail> SearchByIdAdmin(Long idAdmin, int page, int size);
+
+    public Page<Trail> SearchByTarget(String target, int page, int size);
+
+    public Page<Trail> SearchByIdTarget(Long idTarget, int page, int size);
+
+    public Page<Trail> SearchByTargetAndIdTarget(String target, Long idTarget, int page, int size);
+
+    public Trail Insert(Trail trail);
+}