ViewRegistry { @Transactional protected void setViewInstanceRoleAccess(ViewInstanceEntity viewInstanceEntity, Collection<String> roles) { if ((roles != null) && !roles.isEmpty()) { PermissionEntity permissionViewUser = permissionDAO.findViewUsePermission(); ResourceEntity resourceEntity = viewInstanceEntity.getResource(); if (null == resourceEntity) { resourceEntity = instanceDAO.findResourceForViewInstance(viewInstanceEntity.getViewName(), viewInstanceEntity.getInstanceName()); } if (permissionViewUser == null) { LOG.error("Missing the {} role. Access to view cannot be set.", PermissionEntity.VIEW_USER_PERMISSION_NAME, viewInstanceEntity.getName()); } else { for (String role : roles) { PermissionEntity permissionRole = permissionDAO.findByName(role); if (permissionRole == null) { LOG.warn("Invalid role {} encountered while setting access to view {}, Ignoring.", role, viewInstanceEntity.getName()); } else { PrincipalEntity principalRole = permissionRole.getPrincipal(); if (principalRole == null) { LOG.warn("Missing principal ID for role {} encountered while setting access to view {}. Ignoring.", role, viewInstanceEntity.getName()); } else if (!privilegeDAO.exists(principalRole, resourceEntity, permissionViewUser)) { PrivilegeEntity privilegeEntity = new PrivilegeEntity(); privilegeEntity.setPermission(permissionViewUser); privilegeEntity.setPrincipal(principalRole); privilegeEntity.setResource(resourceEntity); privilegeDAO.create(privilegeEntity); } } } } } } @Inject  ViewRegistry(AmbariEventPublisher publisher); static void main(String[] args); Collection<ViewEntity> getDefinitions(); ViewEntity getDefinition(String viewName, String version); ViewEntity getDefinition(ResourceTypeEntity resourceTypeEntity); void addDefinition(ViewEntity definition); Collection<ViewInstanceEntity> getInstanceDefinitions(ViewEntity definition); ViewInstanceEntity getInstanceDefinition(String viewName, String version, String instanceName); void addInstanceDefinition(ViewEntity definition, ViewInstanceEntity instanceDefinition); void removeInstanceDefinition(ViewEntity definition, String instanceName); static void initInstance(ViewRegistry singleton); static ViewRegistry getInstance(); Set<SubResourceDefinition> getSubResourceDefinitions(
      String viewName, String version); void readViewArchives(); void readViewArchives(String viewNameRegExp); boolean instanceExists(ViewInstanceEntity instanceEntity); void installViewInstance(ViewInstanceEntity instanceEntity); void updateViewInstance(ViewInstanceEntity instanceEntity); void updateView(ViewInstanceEntity instanceEntity); ViewInstanceEntity getViewInstanceEntity(String viewName, String instanceName); @Transactional void uninstallViewInstance(ViewInstanceEntity instanceEntity); @Transactional void removeInstanceData(ViewInstanceEntity instanceEntity, String key); @Transactional void copyPrivileges(ViewInstanceEntity sourceInstanceEntity,
                             ViewInstanceEntity targetInstanceEntity); void fireEvent(Event event); synchronized void registerListener(Listener listener, String viewName, String viewVersion); synchronized void unregisterListener(Listener listener, String viewName, String viewVersion); boolean hasPermission(PermissionEntity permissionEntity, ResourceEntity resourceEntity, String userName); boolean checkPermission(String viewName, String version, String instanceName, boolean readOnly); boolean checkPermission(ViewInstanceEntity instanceEntity, boolean readOnly); boolean checkAdmin(); boolean includeDefinition(ViewEntity definitionEntity); void setViewInstanceProperties(ViewInstanceEntity instanceEntity, Map<String, String> properties,
                                        ViewConfig viewConfig, ClassLoader classLoader); Cluster getCluster(ViewInstanceDefinition viewInstance); @Subscribe @AllowConcurrentEvents void onAmbariEvent(ServiceInstalledEvent event); @Subscribe void onClusterConfigFinishedEvent(ClusterConfigFinishedEvent event); void readViewArchive(Path path); static final String API_PREFIX; static final String DEFAULT_AUTO_INSTANCE_URL; }
@Test public void testSetViewInstanceRoleAccess() throws Exception { final Map<String, PermissionEntity> permissions = new HashMap<>(); permissions.put("CLUSTER.ADMINISTRATOR", TestAuthenticationFactory.createClusterAdministratorPermission()); permissions.put("CLUSTER.OPERATOR", TestAuthenticationFactory.createClusterOperatorPermission()); permissions.put("SERVICE.ADMINISTRATOR", TestAuthenticationFactory.createServiceAdministratorPermission()); permissions.put("SERVICE.OPERATOR", TestAuthenticationFactory.createServiceOperatorPermission()); permissions.put("CLUSTER.USER", TestAuthenticationFactory.createClusterUserPermission()); PermissionEntity permissionViewUser = TestAuthenticationFactory.createViewUserPermission(); ViewInstanceEntity viewInstanceEntity = ViewInstanceEntityTest.getViewInstanceEntity(); ResourceEntity resourceEntity = viewInstanceEntity.getResource(); Map<String, PrivilegeEntity> expectedPrivileges = new HashMap<>(); for (Map.Entry<String, PermissionEntity> entry : permissions.entrySet()) { if(!entry.getKey().equals("CLUSTER.ADMINISTRATOR")) { expectedPrivileges.put(entry.getKey(), TestAuthenticationFactory.createPrivilegeEntity(resourceEntity, permissionViewUser, entry.getValue().getPrincipal())); } } Capture<PrivilegeEntity> captureCreatedPrivilegeEntity = Capture.newInstance(CaptureType.ALL); for (Map.Entry<String, PermissionEntity> entry : permissions.entrySet()) { expect(permissionDAO.findByName(entry.getKey())).andReturn(entry.getValue()).atLeastOnce(); } expect(permissionDAO.findViewUsePermission()).andReturn(permissionViewUser).atLeastOnce(); expect(privilegeDAO.exists(EasyMock.anyObject(PrincipalEntity.class), eq(resourceEntity), eq(permissionViewUser))) .andAnswer(new IAnswer<Boolean>() { @Override public Boolean answer() throws Throwable { return EasyMock.getCurrentArguments()[0] == permissions.get("CLUSTER.ADMINISTRATOR").getPrincipal(); } }) .anyTimes(); privilegeDAO.create(capture(captureCreatedPrivilegeEntity)); expectLastCall().times(expectedPrivileges.size()); replay(privilegeDAO, permissionDAO); ViewRegistry viewRegistry = ViewRegistry.getInstance(); viewRegistry.setViewInstanceRoleAccess(viewInstanceEntity, permissions.keySet()); verify(privilegeDAO, permissionDAO); Assert.assertTrue(expectedPrivileges.size() != permissions.size()); Assert.assertTrue(captureCreatedPrivilegeEntity.hasCaptured()); List<PrivilegeEntity> capturedValues = captureCreatedPrivilegeEntity.getValues(); Assert.assertNotNull( capturedValues); Set<PrivilegeEntity> uniqueCapturedValues = new HashSet<>(capturedValues); Assert.assertEquals(expectedPrivileges.size(), uniqueCapturedValues.size()); for(PrivilegeEntity capturedValue: uniqueCapturedValues) { Assert.assertTrue(expectedPrivileges.containsValue(capturedValue)); } }