Project

General

Profile

« Previous | Next » 

Revision 9190

Added by Jing Tao about 9 years ago

Refactory the authorize methods on D1NodeService.
Add the check for whom can call mn.updatesystemMetadata.

View differences:

MNodeService.java
2141 2141
		
2142 2142
		return bagInputStream;
2143 2143
	}
2144

  
2144
    
2145
	/**
2146
	 * Update the system metadata of the specified pid.
2147
	 */
2145 2148
	@Override
2146 2149
	public boolean updateSystemMetadata(Session session, Identifier pid,
2147 2150
            SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
......
2149 2152
	 if(sysmeta == null) {
2150 2153
	     throw  new InvalidRequest("4863", "The system metadata object should NOT be null in the updateSystemMetadata request.");
2151 2154
	 }
2155
	 if(pid == null || pid.getValue() == null) {
2156
         throw new InvalidRequest("4863", "Please specify the id in the updateSystemMetadata request ") ;
2157
     }
2158

  
2152 2159
	 if(!isAuthoritativeNode(pid)) {
2153 2160
	     throw  new InvalidRequest("4863", "Client can only call updateSystemMetadata request on the authoritative memember node.");
2154 2161
	 }
2162

  
2163
     if (session == null) {
2164
         //TODO: many of the thrown exceptions do not use the correct error codes
2165
         //check these against the docs and correct them
2166
         throw new NotAuthorized("4861", "No Session - could not authorize for updating system metadata." +
2167
                 "  If you are not logged in, please do so and retry the request.");
2168
     } else {
2169
         try {
2170
             //Following session can do the change:
2171
           //- Authoritative Member Node (we can use isNodeAdmin since we checked isAuthoritativeNode in line 2159)
2172
             //- Owner of object (coved by the userHasPermission method)
2173
             //- user subjects with the change permission
2174
             //Note: Coordinating Node can not because MN is authoritative
2175
             if(!isNodeAdmin(session) && !userHasPermission(session, pid, Permission.CHANGE_PERMISSION)) {
2176
                 throw new NotAuthorized("4861", "The client -"+ session.getSubject().getValue()+ "is not authorized for updating the system metadata of the object "+pid.getValue());
2177
             }
2178
         } catch (NotFound e) {
2179
             throw new InvalidRequest("4863", "Can't determine if the client has the permission to update the system metacat of the object with id "+pid.getValue()+" since "+e.getDescription());
2180
         }
2181
         
2182
     }
2155 2183
      //update the system metadata locally  
2156 2184
      boolean success = super.updateSystemMetadata(session, pid, sysmeta);
2157 2185
      
2158 2186
      if(success) {
2187
          //TODO
2159 2188
          //notify the cns the synchornize the new system metadata.
2160 2189
      }
2161 2190
      return success;
2162 2191
    }
2192
	
2193
	/*
2194
     * Determine if the current node is the authoritative node for the given pid.
2195
     */
2196
    protected boolean isAuthoritativeNode(Identifier pid) {
2197
        boolean isAuthoritativeNode = false;
2198
        if(pid != null && pid.getValue() != null) {
2199
            SystemMetadata sys = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
2200
            if(sys != null) {
2201
                NodeReference node = sys.getAuthoritativeMemberNode();
2202
                if(node != null) {
2203
                    String nodeValue = node.getValue();
2204
                    logMetacat.debug("The authoritative node for id "+pid.getValue()+" is "+nodeValue);
2205
                    //System.out.println("The authoritative node for id "+pid.getValue()+" is "+nodeValue);
2206
                    String currentNodeId = Settings.getConfiguration().getString("dataone.nodeId");
2207
                    logMetacat.debug("The node id in metacat.properties is "+currentNodeId);
2208
                    //System.out.println("The node id in metacat.properties is "+currentNodeId);
2209
                    if(currentNodeId != null && !currentNodeId.trim().equals("") && currentNodeId.equals(nodeValue)) {
2210
                        logMetacat.debug("They are matching");
2211
                        //System.out.println("They are matching");
2212
                        isAuthoritativeNode = true;
2213
                    }
2214
                }
2215
            }
2216
        }
2217
        return isAuthoritativeNode;
2218
    }
2163 2219
    
2164 2220
}

Also available in: Unified diff