class ObjectPermissionServiceImpl extends HibernateDaoImpl implements
				ObjectPermissionService, ObjectPermissionServiceInternal,
				ApplicationContextAware, PersistentObjectResolver, InitializingBean {

	@SuppressWarnings("rawtypes")
	@Transactional
	public void putObjectPermission(ExecutionContext context, ObjectPermission objPermission) throws IllegalArgumentException{
        if(objPermission == null){
            throw new IllegalArgumentException("Permission can't be null");
        }

        // checking administrative access
        if (!isPrivilegedOperation(context) && !isObjectAdministrable(context, objPermission.getURI())) {
            throw new AccessDeniedException("Access is denied");
        }
        final Object permissionRecipient = objPermission.getPermissionRecipient();
        if(permissionRecipient == null){
            throw new IllegalArgumentException("Permission recipient can't be null");
        }
        if(permissionRecipient instanceof User){
            final User user = (User) permissionRecipient;
            final String username = user.getUsername();
            if(username == null || username.isEmpty()){
                throw new IllegalArgumentException("User name can't be null");
            }

            Set<String> tenantSet = new HashSet<String>(1);
            tenantSet.add(user.getTenantId());
            final List tenantUsers = userService.getTenantUsers(null, tenantSet, username);
            if(tenantUsers == null || tenantUsers.isEmpty()){
                throw new IllegalArgumentException("User '" + username + "' doesn't exists and can't be used as permissionRecipient for permission");
            }
        } else if(permissionRecipient instanceof Role){
            final Role role = (Role) permissionRecipient;
            final String roleName = role.getRoleName();
            if(roleName == null || roleName.isEmpty()){
                throw new IllegalArgumentException("Role name can't be null");
            }
            Set<String> tenantSet = new HashSet<String>();
            tenantSet.add(role.getTenantId());
            final List roles = userService.getTenantRoles(null, tenantSet, roleName);
            if(roles == null || roles.isEmpty()){
                throw new IllegalArgumentException("Role '" + roleName + "' doesn't exists and can't be used as permissionRecipient for permission");
            }
        } else {
            throw new IllegalArgumentException("Unknown type of permissionRecipient. Invalid permissionRecipient: " + permissionRecipient.toString());
        }
        // Given the object and the recipient, find the permission
		RepoObjectPermission existingPerm = getRepoObjectPermission(context, objPermission);
		if (existingPerm == null) {
			existingPerm = (RepoObjectPermission) getPersistentClassFactory().newObject(ObjectPermission.class);
		}

        addParamsToSetPermissionAuditEvent(context, objPermission, existingPerm.getPermissionMask());

		existingPerm.copyFromClient(objPermission, this);
		getHibernateTemplate().saveOrUpdate(existingPerm);

		clearAclEntriesCache(objPermission.getURI());
	}

}