class RepositoryPermissionServiceImpl implements RepositoryPermissionService {

	@Override
	public AccessLevel findTopestPermission(Principal loginUser, Repository project) {
		if(loginUser == null) {
			if(project.getIsPublic()) {
				return AccessLevel.READ;
			}
			return AccessLevel.FORBIDDEN;
		}
		
		// 如果用户登录名不存在
		UserInfo user = userService.findByLoginName(loginUser.getName()).orElseThrow(NoAuthorizationException::new);
		List<RepositoryAuthorization> authes = projectAuthorizationDao.findAllByUserIdAndRepositoryId(user.getId(), project.getId());
		if(authes.isEmpty()) {
			if(project.getIsPublic()) {
				return AccessLevel.READ;
			}
			return AccessLevel.FORBIDDEN;
		}
		
		return authes.stream().map(RepositoryAuthorization::getAccessLevel)
						.max((o1, o2) -> o1.getScore() - o2.getScore()).get();
	}

}