View Javadoc

1   package org.astrogrid.mySpace.mySpaceManager;
2   
3   import java.io.*;
4   
5   // import org.astrogrid.community.common.util.CommunityMessage;
6   // import org.astrogrid.community.delegate.policy.PolicyServiceDelegate;
7   
8   import org.astrogrid.mySpace.mySpaceStatus.MySpaceStatus;
9   import org.astrogrid.mySpace.mySpaceStatus.MySpaceStatusCode;
10  
11  /***
12   * The <code>UserAccount</code> class represents details of an AstroGrid
13   * User within the MySpace system.  It encapsulates details such as his
14   * identifier (ID), community and any privileges that he might have.
15   * <p>
16   * Many of the details of how Users are to be represented are still
17   * unclear, so in Iteration 3 some of the details of the class are
18   * provisional.  Most of the MySpace system merely passes
19   * <code>UserAccount</code> objects about, but does not inquire they
20   * contents, precisely because they may change in future iterations.
21   * </p>
22   * <p>
23   * A <code>UserAccount</code> object is created immediately the MySpace
24   * system in invoked, and is assembled using details passed about the
25   * user.  In future iterations information might be passed allowing the
26   * object to be created directly.
27   *
28   * @author A C Davenhall (Edinburgh)
29   * @since Iteration 3.
30   * @version Iteration 5.
31   *
32   */
33  
34  public class UserAccount
35  {
36  
37  //Public constants defining the permitted codes for attempted read,
38  //write and delete operations.
39  
40     public static final int READ   = 1;      // Read.
41     public static final int WRITE  = 2;      // Write.
42     public static final int DELETE = 3;      // Delete.
43  
44     private String userId;
45     private String communityId;
46     private String credentials;
47     private String userName;
48  
49  //
50  // Constructors.
51  
52  /***
53   * The simplest constructor to create a new <code>UserAccount</code>.
54   * Note that in Iteration 3 all the arguments are simple Strings.
55   *
56   * @param userId User identifier; the user's unique identifier within
57   *   his AstroGrid community.
58   * @param communityId Community identifier; the unique identifier for
59   *   the user's AstroGrid community.
60   * @param credentials The user's credentials; a String specifying the
61   *   operations that he is allowed to perform.
62   */
63  
64     public UserAccount (String userId, String communityId, String credentials)
65     {  this.userId = userId;
66        this.communityId = communityId;
67        this.credentials = credentials;
68        this.userName = null;
69     }
70  
71  /***
72   * A constructor to create a new UserAccount in the case where in
73   * addition to the mandatory parameters a (human-readable) name for
74   * the user is also known.
75   *
76   * @param userId User identifier; the user's unique identifier within
77   *   his AstroGrid community.
78   * @param communityId Community identifier; the unique identifier for
79   *   the user's AstroGrid community.
80   * @param credentials The user's credentials; a String specifying the
81   *   operations that he is allowed to perform.
82   * @param userName The user's name (in a human-readable form)
83   */
84  
85     public UserAccount (String userId, String communityId, String credentials,
86       String userName)
87     {  this.userId = userId;
88        this.communityId = communityId;
89        this.credentials = credentials;
90        this.userName = userName;
91     }
92  
93  //
94  // Methods.
95  
96  /***
97   * Return an identifier for the User which is globally unique within the
98   * AstroGrid system.  The User's identifier is assumed to be unique within
99   * his community and the community identifiers are assumed to be unique.
100  * Therefore a globally unique identifier is created by concatenating the
101  * User identifier and the community identifier.  An `@'is inserted as a
102  * separator between the user and community identifiers.
103  */
104 
105    public String getUserAGrId()
106    {  return userId;
107    }
108 
109 /***
110  * Return the User's real name.
111  */
112 
113    public String getUserName()
114    {  return userName;
115    }
116 
117 /***
118  * Return the User's identifier.
119  */
120 
121    public String getUserId()
122    {  return userId;
123    }
124 
125 /***
126  * Return the User's community identifier.
127  */
128 
129    public String getCommunityId()
130    {  return communityId;
131    }
132 
133 /***
134  * Return the User's credentials.
135  */
136 
137    public String getCredentials()
138    {  return credentials;
139    }
140 
141 /***
142  * Return the User's base container.
143  *
144  * The base container comprises a leading `/' followed by the userId.
145  */
146 
147    public String getBaseContainer()
148    {  return "/" +  userId;
149    }
150 
151 //
152 // Authentication and Authorisation methods.
153 // 
154 // The UserAccount class, and indeed, the MySpace system does not
155 // perform any user authentication.  That is, no checks are made to
156 // ensure that the user is who he says that he is.  Such checks are
157 // assumed to have been made elsewhere in the AstroGrid system.
158 //
159 // There are, however, two methods for checking the user's authorisation.
160 // That is, whether he is permitted to perform the operation that he
161 // is attempting:
162 //
163 // checkAuthorisation: provides fine-grained checks on operations
164 //   attempted on individual dataHolders.  The current implementation
165 //   is a dummy,
166 //
167 // checkSystemAuthorisation: provides a coarse-grained check on whether
168 //   the user is permitted to use the MySpace system for a given class
169 //   of operations.  Currently a simple implementation is available.
170 //
171 // The difference in usage is (typically) that checkAuthorisation would
172 // be invoked every time an operation is attempted on a dataHolder,
173 // whereas checkSystemAuthorisation is be invoked once by every `action'
174 // method in the MySpaceActions class.
175 
176 /***
177  * Check whether the <code>UserAccount</code> has the necessary
178  * privileges to perform a given operation on a given
179  * <code>DataHolder</code>.  The current implementation is a dummy which
180  * always returns true.
181  * 
182  * @param opCode The operation to be performed, coded as follows:
183  * <code>UserAccount.READ</code> - read; <code>UserAccount.WRITE</code> -
184  * write; <code>UserAccount.DELETE</code> - delete.
185  * @param ownerID The identifer of the owner of the <code>DataHolder</code>
186  * on which the operation is to be performed.
187  * @param permissions The permissions mask of the <code>DataHolder</code>
188  * on which the operation is to be performed.
189  * @returns Returns true if the user has the necessary privileges to
190  * perform the requested given operation, otherwise returns false.
191  */
192 
193 // [TODO]: A permissions check is required here.
194 
195   public boolean checkAuthorisation(int opCode, String ownerID,
196     String permissions)
197   {  return true;
198   }
199 
200 /***
201  * Check whether the <code>UserAccount</code> has the necessary
202  * privileges to perform a given class of operation on the current
203  * MySpace system.
204  *
205  * @param opCode <code>UserAccount.READ</code> code for the class of
206  *   operation being attempted.
207  */
208 
209 // [TODO]: A permissions check is required here.
210 
211   public boolean checkSystemAuthorisation(int opCode)
212   {  boolean authorised = true;
213 
214 //
215 //  Check that the MySpace manager is configured to check user
216 //  privileges.
217 
218      Configuration config = new Configuration();
219      if (config.getCHECKPERMISSIONS() )
220      {  String oper = null;
221 
222 //
223 //     Translate the operation codes into the form required by the
224 //     permissions manager.
225 
226         if (opCode == UserAccount.READ)
227         {  oper = "read";
228         }
229         else if (opCode == UserAccount.WRITE)
230         {  oper = "write";
231         }
232         else
233         {  authorised = false;
234 
235            MySpaceStatus status  = new MySpaceStatus(
236              MySpaceStatusCode.AGMMCE00050, MySpaceStatusCode.ERROR,
237              MySpaceStatusCode.NOLOG, this.getClassName() );
238         }
239 
240 //
241 //     If ok then create a permissions manager delegate and check whether
242 //     the user is authorised for this type of operation.
243 
244 //     [TODO]: A permissions check is required here.
245 
246 //      if (authorised)
247 //      {  PolicyServiceDelegate psd = new PolicyServiceDelegate();
248 
249 //         try
250 //         {  String agUserId = this.getUserAGrId();
251 
252 //            authorised = psd.checkPermissions(agUserId, credentials,
253 //              "myspace", oper);
254 //         }
255 //         catch (Exception e)
256 //         {  authorised = false;
257 
258 //            MySpaceStatus status  = new MySpaceStatus(
259 //              MySpaceStatusCode.AGMMCE00050, MySpaceStatusCode.ERROR,
260 //              MySpaceStatusCode.NOLOG, this.getClassName() );
261 //         }
262 //      }
263      }
264 
265      return authorised;
266   }
267 
268 
269 /***
270   * Check that the user can modify (create, change, delete) new users
271   * of the MySpace system.
272   */
273 
274 // [TODO]: A permissions check is required here.
275 
276   public boolean checkCanModifyUsers()
277   {  return true;
278   }
279 
280 /***
281  * Produce a reasonable string representation of a
282  * <code>UserAccount</code>.
283  */
284 
285    public String toString()
286    {  String userRepn = null;
287 
288       if (userName != null)
289       {  userRepn = userId + " (" + userName + ")";
290       }
291       else
292       {  userRepn = userId;
293       }
294 
295       return userRepn;
296    }
297 
298 /***
299  * Obtain the name of the current Java class.
300  */
301 
302    protected String getClassName()
303    { Class currentClass = this.getClass();
304      String name =  currentClass.getName();
305      int dotPos = name.lastIndexOf(".");
306      if (dotPos > -1)
307      {  name = name.substring(dotPos+1, name.length() );
308      }
309 
310      return name;
311    }
312 }