Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Privilege service,repo,controller and test in Portal
- Loading branch information
Showing
11 changed files
with
230 additions
and
27 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/PrivilegeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ctrip.apollo.portal.controller; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/privileges") | ||
public class PrivilegeController { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apollo-portal/src/main/java/com/ctrip/apollo/portal/entity/Privilege.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.ctrip.apollo.portal.entity; | ||
|
||
import java.io.Serializable; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class Privilege implements Serializable { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = -430087307622435118L; | ||
|
||
@Id | ||
@GeneratedValue | ||
private long id; | ||
|
||
@Column | ||
private String name; | ||
|
||
@Column | ||
private String privilType; | ||
|
||
@Column | ||
private String appId; | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getPrivilType() { | ||
return privilType; | ||
} | ||
|
||
public void setPrivilType(String privilType) { | ||
this.privilType = privilType; | ||
} | ||
|
||
public String getAppId() { | ||
return appId; | ||
} | ||
|
||
public void setAppId(String appId) { | ||
this.appId = appId; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
apollo-portal/src/main/java/com/ctrip/apollo/portal/repository/PrivilegeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.ctrip.apollo.portal.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.repository.PagingAndSortingRepository; | ||
|
||
import com.ctrip.apollo.portal.entity.Privilege; | ||
|
||
public interface PrivilegeRepository extends PagingAndSortingRepository<Privilege, Long> { | ||
|
||
List<Privilege> findByAppId(String appId); | ||
|
||
Privilege findByAppIdAndPrivilType(String appId, String privilType); | ||
} |
41 changes: 41 additions & 0 deletions
41
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/PrivilegeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.ctrip.apollo.portal.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.ctrip.apollo.portal.entity.Privilege; | ||
import com.ctrip.apollo.portal.repository.PrivilegeRepository; | ||
|
||
@Service | ||
public class PrivilegeService { | ||
|
||
enum PrivilType { | ||
EDIT, REVIEW, RELEASE | ||
} | ||
|
||
@Autowired | ||
private PrivilegeRepository privilRepo; | ||
|
||
public boolean hasPrivilege(String appId, String name, PrivilType privilType) { | ||
Privilege privil = privilRepo.findByAppIdAndPrivilType(appId, privilType.name()); | ||
if (privil != null && privil.getName().equals(name)) return true; | ||
return false; | ||
} | ||
|
||
public List<Privilege> listPrivileges(String appId) { | ||
return privilRepo.findByAppId(appId); | ||
} | ||
|
||
public Privilege setPrivilege(String appId, String name, PrivilType privilType) { | ||
Privilege privil = privilRepo.findByAppIdAndPrivilType(appId, privilType.name()); | ||
if (privil == null) { | ||
privil = new Privilege(); | ||
privil.setAppId(appId); | ||
privil.setPrivilType(privilType.name()); | ||
} | ||
privil.setName(name); | ||
return privilRepo.save(privil); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
apollo-portal/src/test/java/com/ctrip/apollo/portal/AllTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ctrip.apollo.portal; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.junit.runners.Suite.SuiteClasses; | ||
|
||
import com.ctrip.apollo.portal.controller.AppControllerTest; | ||
import com.ctrip.apollo.portal.repository.AppRepositoryTest; | ||
import com.ctrip.apollo.portal.service.PrivilegeServiceTest; | ||
|
||
@RunWith(Suite.class) | ||
@SuiteClasses({AppControllerTest.class, AppRepositoryTest.class, PrivilegeServiceTest.class, | ||
|
||
}) | ||
public class AllTests { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
apollo-portal/src/test/java/com/ctrip/apollo/portal/service/PrivilegeServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.ctrip.apollo.portal.service; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.ctrip.apollo.portal.AbstractPortalTest; | ||
import com.ctrip.apollo.portal.entity.App; | ||
import com.ctrip.apollo.portal.entity.Privilege; | ||
|
||
public class PrivilegeServiceTest extends AbstractPortalTest { | ||
|
||
@Autowired | ||
AppService appService; | ||
|
||
@Autowired | ||
PrivilegeService privilService; | ||
|
||
@Test | ||
public void testAddPrivilege() { | ||
App newApp = new App(); | ||
newApp.setAppId(String.valueOf(System.currentTimeMillis())); | ||
newApp.setName("new app " + System.currentTimeMillis()); | ||
newApp.setOwner("owner " + System.currentTimeMillis()); | ||
appService.save(newApp); | ||
|
||
privilService.setPrivilege(newApp.getAppId(), newApp.getOwner(), | ||
PrivilegeService.PrivilType.EDIT); | ||
List<Privilege> privileges = privilService.listPrivileges(newApp.getAppId()); | ||
Assert.assertEquals(1, privileges.size()); | ||
Assert.assertEquals(PrivilegeService.PrivilType.EDIT.name(), privileges.get(0).getPrivilType()); | ||
Assert.assertEquals(newApp.getOwner(), privileges.get(0).getName()); | ||
} | ||
|
||
@Test | ||
public void testCheckPrivilege() { | ||
App newApp = new App(); | ||
newApp.setAppId(String.valueOf(System.currentTimeMillis())); | ||
newApp.setName("new app " + System.currentTimeMillis()); | ||
newApp.setOwner("owner " + System.currentTimeMillis()); | ||
appService.save(newApp); | ||
|
||
privilService.setPrivilege(newApp.getAppId(), newApp.getOwner(), | ||
PrivilegeService.PrivilType.EDIT); | ||
Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), | ||
PrivilegeService.PrivilType.EDIT)); | ||
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), | ||
PrivilegeService.PrivilType.REVIEW)); | ||
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), | ||
PrivilegeService.PrivilType.RELEASE)); | ||
|
||
privilService.setPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT); | ||
Assert.assertTrue( | ||
privilService.hasPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT)); | ||
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), | ||
PrivilegeService.PrivilType.EDIT)); | ||
|
||
privilService.setPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.RELEASE); | ||
Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), "nobody", | ||
PrivilegeService.PrivilType.RELEASE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters