components/visual-panels/usermgr/src/java/vpanels/app/usermgr/com/oracle/solaris/vp/panels/usermgr/client/swing/UserMgrPanelDescriptor.java
changeset 843 190d2b5889a8
parent 827 0944d8c0158b
child 894 8e0753ec3941
equal deleted inserted replaced
842:9da05efee3cd 843:190d2b5889a8
    44 import com.oracle.solaris.vp.util.misc.property.*;
    44 import com.oracle.solaris.vp.util.misc.property.*;
    45 import com.oracle.solaris.vp.util.swing.HasIcons;
    45 import com.oracle.solaris.vp.util.swing.HasIcons;
    46 
    46 
    47 public class UserMgrPanelDescriptor
    47 public class UserMgrPanelDescriptor
    48     extends AbstractPanelDescriptor<UserManagedObject>
    48     extends AbstractPanelDescriptor<UserManagedObject>
    49     implements SwingPanelDescriptor<UserManagedObject>, HasIcons {
    49     implements SwingPanelDescriptor<UserManagedObject>, HasIcons,
       
    50     ConnectionListener {
    50 
    51 
    51     //
    52     //
    52     // Static data
    53     // Static data
    53     //
    54     //
    54 
    55 
    60 
    61 
    61     public static final String USER_TYPE_NORMAL = "normal";
    62     public static final String USER_TYPE_NORMAL = "normal";
    62     public static final String SCOPE_FILES = "files";
    63     public static final String SCOPE_FILES = "files";
    63     public static final String MATCH_ALL = "*";
    64     public static final String MATCH_ALL = "*";
    64 
    65 
       
    66     public static final String PASSWORD = "PASSWORD";
       
    67     public static final String NOTACTIVATED = "NOTACTIVATED";
       
    68     public static final String LOCKED = "LOCKED";
       
    69     public static final String UNKNOWN = "UNKNOWN";
       
    70 
    65     //
    71     //
    66     // Instance data
    72     // Instance data
    67     //
    73     //
    68 
    74 
    69     private MainControl mc;
    75     private MainControl mc;
    73 
    79 
    74     private String scopeStr = SCOPE_FILES;
    80     private String scopeStr = SCOPE_FILES;
    75     private String typeStr = USER_TYPE_NORMAL;
    81     private String typeStr = USER_TYPE_NORMAL;
    76     private String matchStr = MATCH_ALL;
    82     private String matchStr = MATCH_ALL;
    77 
    83 
    78     private List<UserManagedObject> deleteList =
    84     // Assignable lists
    79 	new ArrayList<UserManagedObject> ();
    85     private List<String> scopeList = null;
    80     private List<UserManagedObject> addList =
    86     private List<String> shellList = null;
    81 	new ArrayList<UserManagedObject> ();
    87     private List<Group> groupList = null;
    82 
    88     private List<String> profileList = null;
    83     private MutableProperty<Integer> addedProperty =
    89     private List<String> authList = null;
    84 	new IntegerProperty();
    90     private List<String> roleList = null;
    85     private MutableProperty<Integer> deletedProperty =
    91     private List<String> supplgroups = null;
    86 	new IntegerProperty();
    92 
    87     {
    93     private User defUser = null;
    88 	getChangeableAggregator().addChangeables(
       
    89 	    addedProperty, deletedProperty);
       
    90     }
       
    91 
    94 
    92     //
    95     //
    93     // Constructors
    96     // Constructors
    94     //
    97     //
    95 
    98 
   102      *
   105      *
   103      * @param	    context
   106      * @param	    context
   104      *		    a handle to interact with the Visual Panels client
   107      *		    a handle to interact with the Visual Panels client
   105      */
   108      */
   106     public UserMgrPanelDescriptor(String id, ClientContext context)
   109     public UserMgrPanelDescriptor(String id, ClientContext context)
   107 	throws TrackerException {
   110 	throws TrackerException, ActionFailedException {
   108 
   111 
   109         super(id, context);
   112         super(id, context);
   110 
   113 
   111 	beanTracker = new MXBeanTracker<UserMgrMXBean>(
   114 	beanTracker = new MXBeanTracker<UserMgrMXBean>(
   112 	    OBJECT_NAME, UserMgrMXBean.class, Stability.PRIVATE, context);
   115 	    OBJECT_NAME, UserMgrMXBean.class, Stability.PRIVATE, context);
   113 
   116 
   114 	setComparator(SimpleHasId.COMPARATOR);
   117 	setComparator(SimpleHasId.COMPARATOR);
   115 
   118 
   116 	// Initialize list of users
   119 	// Initialize list of users
   117 	initUsers(SCOPE_FILES, USER_TYPE_NORMAL, MATCH_ALL);
   120 	initUsers(SCOPE_FILES, USER_TYPE_NORMAL, MATCH_ALL);
   118 
       
   119 	// Keep track of users added/deleted
       
   120 	addedProperty.update(0, true);
       
   121 	deletedProperty.update(0, true);
       
   122 
   121 
   123         control = new PanelFrameControl<UserMgrPanelDescriptor>(this);
   122         control = new PanelFrameControl<UserMgrPanelDescriptor>(this);
   124         mc = new MainControl(this);
   123         mc = new MainControl(this);
   125 	control.addChildren(mc);
   124 	control.addChildren(mc);
       
   125 	context.addConnectionListener(this);
   126     }
   126     }
   127 
   127 
   128     //
   128     //
   129     // PanelDescriptor methods
   129     // PanelDescriptor methods
   130     //
   130     //
   227 
   227 
   228     public void addUserManagedObject(UserManagedObject toAdd) {
   228     public void addUserManagedObject(UserManagedObject toAdd) {
   229 	addChildren(toAdd);
   229 	addChildren(toAdd);
   230     }
   230     }
   231 
   231 
   232     public void addToAddList(UserManagedObject toAdd) {
   232     public void saveDeletedUser(UserManagedObject umo)
   233 	addedProperty.setValue(addedProperty.getValue() + 1);
   233         throws ActionAbortedException,
   234 	addList.add(toAdd);
       
   235     }
       
   236 
       
   237     public void addToDeleteList(UserManagedObject toRemove) {
       
   238 	deletedProperty.setValue(deletedProperty.getValue() + 1);
       
   239 	deleteList.add(toRemove);
       
   240     }
       
   241 
       
   242     public void saveDeletedUsers() throws ActionAbortedException,
       
   243 	ActionFailedException, ActionUnauthorizedException {
   234 	ActionFailedException, ActionUnauthorizedException {
   244 
   235 
   245 	Iterator<UserManagedObject> it = deleteList.iterator();
   236 	try {
   246 	while (it.hasNext()) {
   237 	    getUserMgrBean().deleteUser(umo.getName());
   247 	    UserManagedObject umo = it.next();
   238 	    deleteUserManagedObject(umo);
   248 	    try {
   239 	} catch (SecurityException se) {
   249 		getUserMgrBean().deleteUser(umo.getName());
   240 	    throw new ActionUnauthorizedException(se);
   250 		deleteUserManagedObject(umo);
   241 	} catch (ObjectException e) {
   251 		it.remove();
   242 	    UserMgrError ume = e.getPayload(UserMgrError.class);
   252 		deletedProperty.setValue(deletedProperty.getValue() - 1);
   243 	    String msg = Finder.getString("usermgr.error.invalidData");
   253 	    } catch (SecurityException se) {
   244 	    String err = Finder.getString(
   254 		throw new ActionUnauthorizedException(se);
   245 		"usermgr.error.delete", umo.getUsername());
   255 	    } catch (ObjectException e) {
   246 	    getLog().log(Level.SEVERE, err + msg, e);
   256 		UserMgrError ume = e.getPayload(UserMgrError.class);
   247 	    throw new ActionFailedException(err + msg);
   257 		String msg = Finder.getString("usermgr.error.invalidData");
   248 	// Any other remaining exceptions
   258 		String err = Finder.getString(
   249 	} catch (Exception e) {
   259 		    "usermgr.error.delete", umo.getUsername());
   250 	    String msg = Finder.getString("usermgr.error.system");
   260 		getLog().log(Level.SEVERE, err + msg, e);
   251 	    String err = Finder.getString(
   261 		throw new ActionFailedException(err + msg);
   252 		"usermgr.error.delete", umo.getUsername());
   262 	    // Any other remaining exceptions
   253 	    getLog().log(Level.SEVERE, err + msg, e);
   263 	    } catch (Exception e) {
   254 	    throw new ActionFailedException(err + msg);
   264 		String msg = Finder.getString("usermgr.error.system");
   255 	}
   265 		String err = Finder.getString(
   256     }
   266 		    "usermgr.error.delete", umo.getUsername());
   257 
   267 		getLog().log(Level.SEVERE, err + msg, e);
   258     public void saveAddedUser(UserManagedObject umo)
   268 		throw new ActionFailedException(err + msg);
   259         throws ActionAbortedException,
   269 	    }
       
   270 	}
       
   271     }
       
   272 
       
   273     public void saveAddedUsers() throws ActionAbortedException,
       
   274 	ActionFailedException, ActionUnauthorizedException {
   260 	ActionFailedException, ActionUnauthorizedException {
   275 
   261 
   276 	Iterator<UserManagedObject> it = addList.iterator();
   262 	try {
   277 	while (it.hasNext()) {
   263 	    char[] password = umo.getPassword();
   278 	    UserManagedObject umo = it.next();
   264 	    User user = getUserMgrBean().addUser(umo.getNewUser(), password);
   279 	    try {
   265 	    addUserManagedObject(umo);
   280 		char[] password = umo.getPassword();
   266 	    Arrays.fill(password, (char)0);
   281 		User user = getUserMgrBean().addUser(
   267 	    umo.updateUser(user);
   282 		    umo.getNewUser(), password);
   268 
   283 		addUserManagedObject(umo);
   269 	} catch (SecurityException se) {
   284 		Arrays.fill(password, (char)0);
   270 	    throw new ActionUnauthorizedException(se);
   285 
   271 	} catch (ObjectException e) {
   286 		it.remove();
       
   287 		addedProperty.setValue(addedProperty.getValue() - 1);
       
   288 		umo.updateUser(user);
       
   289 	    } catch (SecurityException se) {
       
   290 		throw new ActionUnauthorizedException(se);
       
   291 	    } catch (ObjectException e) {
       
   292 		e.printStackTrace();
       
   293 		UserMgrError ume = e.getPayload(UserMgrError.class);
   272 		UserMgrError ume = e.getPayload(UserMgrError.class);
   294 		String msg;
   273 		String msg;
   295 		UserMgrErrorType error = (ume != null) ?
   274 		UserMgrErrorType error = (ume != null) ?
   296 		    ume.getErrorCode() : UserMgrErrorType.INVALIDDATA;
   275 		    ume.getErrorCode() : UserMgrErrorType.INVALIDDATA;
   297 		switch (error) {
   276 		switch (error) {
   308 		String err = Finder.getString("usermgr.error.add",
   287 		String err = Finder.getString("usermgr.error.add",
   309 		    umo.getUsername());
   288 		    umo.getUsername());
   310 		getLog().log(Level.SEVERE, err + msg, e);
   289 		getLog().log(Level.SEVERE, err + msg, e);
   311     		deleteUserManagedObject(umo);
   290     		deleteUserManagedObject(umo);
   312 		throw new ActionFailedException(err + msg);
   291 		throw new ActionFailedException(err + msg);
   313 	    // Any other remaining exceptions
   292 	// Any other remaining exceptions
   314 	    } catch (Exception e) {
   293 	} catch (Exception e) {
   315 		String msg = Finder.getString("usermgr.error.system");
   294 		String msg = Finder.getString("usermgr.error.system");
   316 		String err = Finder.getString(
   295 		String err = Finder.getString(
   317 		    "usermgr.error.add", umo.getUsername());
   296 		    "usermgr.error.add", umo.getUsername());
   318 		getLog().log(Level.SEVERE, err + msg, e);
   297 		getLog().log(Level.SEVERE, err + msg, e);
   319 		throw new ActionFailedException(err + msg);
   298 		throw new ActionFailedException(err + msg);
   320 	    }
       
   321 	    umo.getChangeableAggregator().save();
       
   322 	}
   299 	}
   323     }
   300     }
   324 
   301 
   325     public void saveModifiedUsers() throws ActionAbortedException,
   302     public void saveModifiedUsers() throws ActionAbortedException,
   326 	ActionFailedException, ActionUnauthorizedException {
   303 	ActionFailedException, ActionUnauthorizedException {
   370 	    }
   347 	    }
   371 	}
   348 	}
   372     }
   349     }
   373 
   350 
   374     public List<Group> getGroups() {
   351     public List<Group> getGroups() {
   375 	try {
   352 	return groupList;
   376 	    return getUserMgrBean().getgroups();
       
   377         } catch (ObjectException e) {
       
   378             getLog().log(Level.SEVERE, "Error getting group list.", e);
       
   379 	}
       
   380 	return null;
       
   381     }
   353     }
   382 
   354 
   383     public List<String> getSupplGroups() {
   355     public List<String> getSupplGroups() {
   384 	try {
   356 	return supplgroups;
   385 	    return getUserMgrBean().getsupplGroups();
       
   386         } catch (ObjectException e) {
       
   387             getLog().log(Level.SEVERE,
       
   388 	    "Error getting supplementary group list.", e);
       
   389 	}
       
   390 	return null;
       
   391     }
   357     }
   392 
   358 
   393     public List<String> getShells() {
   359     public List<String> getShells() {
   394 	try {
   360         return shellList;
   395 	    return getUserMgrBean().getshells();
       
   396         } catch (ObjectException e) {
       
   397             getLog().log(Level.SEVERE, "Error getting shell list.", e);
       
   398 	}
       
   399 	return null;
       
   400     }
   361     }
   401 
   362 
   402     public List<String> getScopes() {
   363     public List<String> getScopes() {
   403 	try {
   364         return scopeList;
   404 	    return getUserMgrBean().getscopes();
       
   405         } catch (ObjectException e) {
       
   406             getLog().log(Level.SEVERE, "Error getting  scopes list.", e);
       
   407 	}
       
   408 	return null;
       
   409     }
   365     }
   410 
   366 
   411     public List<String> getProfiles() {
   367     public List<String> getProfiles() {
   412 	try {
   368         return profileList;
   413 	    return getUserMgrBean().getprofiles();
       
   414         } catch (ObjectException e) {
       
   415             getLog().log(Level.SEVERE, "Error getting profiles list.", e);
       
   416 	}
       
   417 	return null;
       
   418     }
   369     }
   419 
   370 
   420     public List<String> getAuths() {
   371     public List<String> getAuths() {
   421 	try {
   372         return authList;
   422 	    return getUserMgrBean().getauths();
       
   423         } catch (ObjectException e) {
       
   424             getLog().log(Level.SEVERE, "Error getting authorizations list.", e);
       
   425 	}
       
   426 	return null;
       
   427     }
   373     }
   428 
   374 
   429     public List<String> getRoles() {
   375     public List<String> getRoles() {
   430 	try {
   376         return roleList;
   431 	    return getUserMgrBean().getroles();
       
   432         } catch (ObjectException e) {
       
   433             getLog().log(Level.SEVERE, "Error getting roles list.", e);
       
   434 	}
       
   435 	return null;
       
   436     }
       
   437 
       
   438     public void setScope(String scope) {
       
   439         ScopeType sType;
       
   440 	if (scope.equals(SCOPE_FILES)) {
       
   441 	    sType = ScopeType.FILES;
       
   442 	} else {
       
   443 	    sType = ScopeType.LDAP;
       
   444 	}
       
   445 
       
   446 	try {
       
   447 	    getUserMgrBean().setScope(sType);
       
   448         } catch (Exception e) {
       
   449             getLog().log(Level.SEVERE, "Error setting  scope.", e);
       
   450 	}
       
   451     }
   377     }
   452 
   378 
   453     public UserImpl getDefaultUser() {
   379     public UserImpl getDefaultUser() {
   454 	try {
   380 	UserImpl defaultUser = new UserImpl(
   455 	    User defUser = getUserMgrBean().getdefaultUser();
       
   456 	    return new UserImpl(
       
   457 		"", 0L, defUser.getGroupID(),
   381 		"", 0L, defUser.getGroupID(),
   458 		"", "", defUser.getDefaultShell(),
   382 		"", "", defUser.getDefaultShell(),
   459 		0, 0, 0, 0,
   383 		0, 0, 0, 0,
   460 		"", "", "", "", "", "",
   384 		"", "", "", "", "", "",
   461 		"", "", "", "", "", "",
   385 		"", "", "", "", "", "",
   462 		null, null, null, null, null, null);
   386 		null, null, null, null, null, null);
   463 	} catch (ObjectException e) {
   387 
   464 	    getLog().log(Level.SEVERE, "Error getting default user.", e);
   388         return defaultUser;
   465 	}
       
   466 
       
   467 	return null;
       
   468     }
   389     }
   469 
   390 
   470     public void initUsers(String scopeStr,
   391     public void initUsers(String scopeStr,
   471     		String typeStr, String matchStr) {
   392     		String typeStr, String matchStr)
       
   393 		throws ActionFailedException {
   472 	int count = 0;
   394 	int count = 0;
   473 	String statusStr;
   395 	String statusStr;
   474 	String listTitle;
   396 	String listTitle;
   475 
   397 
   476 	this.scopeStr = scopeStr;
   398 
   477 	this.typeStr = typeStr;
       
   478 	this.matchStr = matchStr;
       
   479 
       
   480 	setScope(scopeStr);
       
   481 
   399 
   482 	statusStr = Finder.getString("usermgr.status.scope") +
   400 	statusStr = Finder.getString("usermgr.status.scope") +
   483 	    " " + scopeStr;
   401 	    " " + scopeStr;
   484 	if (typeStr.equals(USER_TYPE_NORMAL)) {
   402 	if (typeStr.equals(USER_TYPE_NORMAL)) {
   485 	    uType = UserType.NORMAL;
   403 	    uType = UserType.NORMAL;
   487 	} else {
   405 	} else {
   488 	    uType = UserType.ROLE;
   406 	    uType = UserType.ROLE;
   489 	    listTitle = Finder.getString("usermgr.list.title.role");
   407 	    listTitle = Finder.getString("usermgr.list.title.role");
   490 	}
   408 	}
   491 
   409 
   492 	setFilter(uType, matchStr);
       
   493 	List<User> users = getUsers();
       
   494 
   410 
   495 	removeAllChildren();
   411 	removeAllChildren();
   496 	try {
   412 	try {
       
   413 	    UserMgrMXBean bean = getUserMgrBean();
       
   414 
       
   415 	    // Set scope only if the scope changed
       
   416 	    if (scopeStr.equals(this.scopeStr) == false) {
       
   417 		ScopeType sType;
       
   418 		if (scopeStr.equals(SCOPE_FILES)) {
       
   419 		    sType = ScopeType.FILES;
       
   420 		} else {
       
   421 		    sType = ScopeType.LDAP;
       
   422 		}
       
   423 		bean.selectScope(sType);
       
   424 		this.scopeStr = scopeStr;
       
   425 	    }
       
   426 
       
   427 	    setFilter(uType, matchStr);
       
   428 	    this.typeStr = typeStr;
       
   429 	    this.matchStr = matchStr;
       
   430 
       
   431 	    List<User> users = getUsers();
       
   432 
   497 	    boolean uTypeSet = false;
   433 	    boolean uTypeSet = false;
       
   434 
   498 	    for (User user : users) {
   435 	    for (User user : users) {
   499 		String username = user.getUsername();
   436 		String username = user.getUsername();
   500 		UserMgrMXBean bean = getUserMgrBean();
       
   501 		if (uTypeSet == false) {
   437 		if (uTypeSet == false) {
   502 		    uType = bean.getUserType(username);
   438 		    uType = bean.getUserType(username);
   503 		    uTypeSet = true;
   439 		    uTypeSet = true;
   504 		}
   440 		}
   505 
   441 
   506 		UserManagedObject umo = new UserManagedObject(this,
   442 		UserManagedObject umo = new UserManagedObject(this,
   507 		    user, uType, null, false);
   443 		    user, uType, null, false);
   508 		addChildren(umo);
   444 		addChildren(umo);
   509 	    }
   445 	    }
   510 
   446 
   511 	} catch (ObjectException e) {
   447 	    // Get Assignable Lists
   512 	    getLog().log(Level.SEVERE, "Error creating user list.", e);
   448 	    scopeList = bean.getscopes();
       
   449 	    groupList = bean.getgroups();
       
   450 	    shellList = bean.getshells();
       
   451 	    authList = bean.getauths();
       
   452 	    profileList = bean.getprofiles();
       
   453 	    roleList = bean.getroles();
       
   454 	    supplgroups = bean.getsupplGroups();
       
   455 	    defUser = bean.getdefaultUser();
       
   456 
       
   457 	} catch (Exception e) {
       
   458 	    String msg = Finder.getString("usermgr.error.system");
       
   459 	    throw new ActionFailedException(msg);
   513 	} finally {
   460 	} finally {
   514 	    setStatusText(statusStr);
   461 	    setStatusText(statusStr);
   515 	    if (mc != null) {
   462 	    if (mc != null) {
   516 		mc.setListTitle(listTitle);
   463 		mc.setListTitle(listTitle);
   517 	    }
   464 	    }
   518         }
   465         }
   519     }
   466     }
   520 
   467 
       
   468     /*
       
   469      * Solaris provides 3 levels of password change:
       
   470      *  1. Can change any password
       
   471      *  2. Can only set initial password
       
   472      *  3. Cannot change any password
       
   473      */
       
   474     public boolean canChangePassword(UserManagedObject umo) {
       
   475 	String statusStr =  defUser.getAccountStatus();
       
   476 
       
   477 	if (statusStr.equals(PASSWORD)) {
       
   478 	    return true;
       
   479 	} else if (statusStr.equals(LOCKED)) {
       
   480             return false;
       
   481 	} else if (statusStr.equals(NOTACTIVATED) && (umo == null ||
       
   482 		umo.getAccountStatus().equals(UNKNOWN) ||
       
   483 		umo.getAccountStatus().equals(NOTACTIVATED))) {
       
   484 	    return true;
       
   485 	}
       
   486 
       
   487         return false;
       
   488     }
       
   489 
   521     public boolean isTypeRole() {
   490     public boolean isTypeRole() {
   522         return (uType == UserType.ROLE ? true : false);
   491         return (uType == UserType.ROLE ? true : false);
   523     }
   492     }
   524 
   493 
   525     public String getTypeString() {
   494     public String getTypeString() {
   537 
   506 
   538     public String getMatch() {
   507     public String getMatch() {
   539         return (matchStr);
   508         return (matchStr);
   540     }
   509     }
   541 
   510 
       
   511     /*
       
   512      * Connection Listener interfaces
       
   513      */
       
   514 
       
   515     /*
       
   516      * If a role assumption or user change occurs because
       
   517      * of permission/auth failure, initialize the users list
       
   518      */
       
   519     public void connectionChanged(ConnectionEvent ce) {
       
   520 	try {
       
   521 	    initUsers(scopeStr, typeStr, matchStr);
       
   522         } catch (Exception e) {
       
   523             getLog().log(Level.SEVERE, "Error setting filter.", e);
       
   524 	}
       
   525     }
       
   526 
       
   527     public void connectionFailed(ConnectionEvent ce) {
       
   528 	setStatusText(Finder.getString("usermgr.error.connfailed"));
       
   529     }
       
   530 
   542     //
   531     //
   543     // Private methods
   532     // Private methods
   544     //
   533     //
   545 
   534 
   546     private List<User> getUsers() {
   535     private List<User> getUsers() {
       
   536 
   547 	List<User> users = null;
   537 	List<User> users = null;
   548 	try {
   538 	try {
   549 	    users = getUserMgrBean().getusers();
   539 	    users = getUserMgrBean().getusers();
   550         } catch (ObjectException e) {
   540         } catch (ObjectException e) {
   551             getLog().log(Level.SEVERE, "Error getting user list.", e);
   541             getLog().log(Level.SEVERE, "Error getting user list.", e);