def delete_group(self, menu_item = None): """Delete the group selected in the group_treeview""" if not self.__check_connection(): return None group, t_iter = self.__get_selected_group() # If nothing is selected we can return and do nothing. # We should only notify the user that what he would like # to do can not be done now. if t_iter is None: show_info_dialog(_("Select a group before asking for its deletion.")) return # Users tend to delete many things they do not want to delete if len(self.__connection.get_members(group)) == 0: question = _("Really delete group <b>%s</b>?") % group else: question = _("Really delete the non empty group <b>%s</b>?" + " This will lead to integrity problems.") % group if not ask_question(question): return # Finally we delete the group try: self.__connection.delete_group(group) except LumInsufficientPermissionsError: show_error_dialog(_("Insufficient permissions to delete group")) return None # and delete the group from the treeview self.__group_store.remove(t_iter) # Finally show the successful operation in the statusbar self.statusbar_update(_("Group %s successfully deleted.") % group)
def edit_user(self, menu_item = None): """Edit selected user""" usermodel, t_iter = self.__get_selected_user() if t_iter is None: show_info_dialog(_("Select a user to modify")) return # Create the dialog making a copy of the old usermodel # so we can check the difference after modification old_user = UserModel(usermodel) dialog = lumEditUserDialog(self.__datapath, usermodel, self.__group_store) new_usermodel = dialog.run() if (new_usermodel is not None): try: self.__connection.modify_user(old_user, new_usermodel) except LumInsufficientPermissionsError: show_error_dialog(_("Insufficient permissions to edit user")) return None self.statusbar_update(_("User %s successfully modified") % new_usermodel.get_username()) # TODO: Reload only selected user self.reload_user_list()
def connect(self, menu_item = None): """Connect to server""" # Determine which server to connect to connect_dialog = lumConnectDialog(self.__datapath, self.__configuration) uri, bind_dn, base_dn, users_ou, groups_ou = connect_dialog.run() # Update internal information self.__uri = uri self.__bind_dn = bind_dn self.__base_dn = base_dn self.__users_ou = users_ou self.__groups_ou = groups_ou if uri is None: return # Add base_dn if necessary if not self.__bind_dn.endswith(self.__base_dn): self.__bind_dn += ",%s" % self.__base_dn if not self.__users_ou.endswith(self.__base_dn): self.__users_ou += ",%s" % self.__base_dn if not self.__groups_ou.endswith(self.__base_dn): self.__groups_ou += ",%s" % self.__base_dn # Get password from keyring password = self.ask_password() # Notify user of connection self.statusbar_update(_("Connecting to %s.") % uri) self.__connection = Connection(uri = self.__uri, bind_dn = self.__bind_dn, password = password, base_dn = self.__base_dn, users_ou = self.__users_ou, groups_ou = self.__groups_ou) self.__connection.connect("missing-ou", self.missing_ou_cb) self.__connection.connect("connection-completed", self.connection_completed_cb) # Try to connect to the specified server try: self.__connection.start() except LumError: # If we can't, maybe password is wrong, so ask it again self.forget_password() password = self.ask_password() # and retry the connection. But if we fail even this time, then # abort try: self.__connection.set_password(password) self.__connection.start() except: # You had two opportunities, and both are gone. show_error_dialog( _("Error while connecting to the server, check your credentials and your connectivity")) self.__connection = None self.statusbar_update(_("Connection failed."))
def __check_connection(self): if self.__connection is None: if ask_question(_("Not connected to any LDAP server, connect now?")): self.connect () else: return False if self.__connection is not None: return True else: show_error_dialog(_("Error while connecting to LDAP server, aborting operation.")) return False
def run(self): """Run dialog""" # Check if the user says "save"... if self.__window.run() == 1: # And then retrieve data username = self.__builder.get_object("username_entry").get_text() givenName = self.__builder.get_object("givenName_entry").get_text() sn = self.__builder.get_object("sn_entry").get_text() home = self.__builder.get_object("home_entry").get_text() shell = self.__builder.get_object("shell_entry").get_text() email = self.__builder.get_object("email_entry").get_text() # Set uid to 0 so ldap_protocol will autodetermine the first free uid # when creating the user uid = 0 group = self.__builder.get_object("group_entry").get_text() gid = self.__connection.gid_from_group(group) # Check if this is an existent user if self.__connection.is_present("uid=%s" % username): show_error_dialog(_("User %s is present, not overwriting it!") % username) self.__window.destroy() return None # Ask the user if he intended to create the group and destroy window # before the call to the ldap module, that could raise an exception # catched by our parent (i.e. lumApp) if gid is None: if ask_question(_("The group %s doesn't exists, create it now?") % group): self.__window.destroy() self.__connection.add_group(group) gid = self.__connection.gid_from_group(group) else: self.__window.destroy() return None else: self.__window.destroy() # Fill UserModel self.usermodel = UserModel() self.usermodel.set_username(username) self.usermodel.set_gid(gid) self.usermodel.set_surname(sn) self.usermodel.set_given_name(givenName) self.usermodel.set_home(home) self.usermodel.set_shell(shell) self.usermodel.set_email(email) else: self.__window.destroy()
def run(self): if self.__dialog.run() == 1: group_name = self.__builder.get_object("group_name_entry").get_text() try: gid = int(self.__builder.get_object("gid_entry").get_text()) except ValueError: show_error_dialog(_("Insert a valid gid")) self.__dialog.destroy() return (None, None) self.__dialog.destroy() return (group_name, gid) else: self.__dialog.destroy() return (None, None)
def create_new_user_dialog(self, menu_item): """Create new user""" if not self.__check_connection(): return None new_user_dialog = lumNewUserDialog(self.__datapath, self.__connection) try: new_user_dialog.run() except LumInsufficientPermissionsError: show_error_dialog(_("Insufficient permissions to accomplish operation")) return None if new_user_dialog.usermodel is not None: if self.__check_connection(): new_user_dialog.usermodel.set_dn("uid=%s,%s" % (new_user_dialog.usermodel.get_username(), self.__users_ou)) try: self.__connection.add_user(new_user_dialog.usermodel) except LumAlreadyExistsError: show_error_dialog(_("User <b>%s</b> already exists in the ldap tree, " + "cannot create one more") % new_user_dialog.usermodel.get_username()) return None except LumInsufficientPermissionsError: show_error_dialog(_("Insufficient permissions to create the user")) return None self.statusbar_update(_("User %s created correctly.") % new_user_dialog.usermodel.get_username()) # Reload user list because the dialog may have created a new group # and then pushing the user will not update the group list too self.reload_user_list()
def new_group(self, menu_item = None): """Create a new group catching the callback from menu""" if not self.__check_connection(): return None new_group_dialog = lumNewGroupDialog(self.__datapath, self.__connection) group_name, gid = new_group_dialog.run() if group_name is not None: try: self.__connection.add_group(group_name, gid) except LumInsufficientPermissionsError: show_error_dialog(_("Insufficient permissions to create group")) return None except LumAlreadyExistsError: show_error_dialog(_("Group <b>%s</b> already exists in the database," + " cannot add one more.") % group_name) return None self.__group_store.append(gid, group_name) self.statusbar_update(_("Group %s successfully created.") % group_name)
def change_password(self, menu_item = None): """Change password of selected user""" usermodel, t_iter = self.__get_selected_user() if t_iter is None: show_info_dialog(_("You need to select a user to change its password")) return password_dialog = lumChangeUserPasswordDialog(self.__datapath, usermodel.get_username()) new_password = password_dialog.run() if new_password is None: return False else: try: self.__connection.change_password(usermodel.get_username(), new_password) except LumInsufficientPermissionsError: show_error_dialog(_("Insufficient permissions to change user password")) return False else: self.statusbar_update(_("Password of user %s changed succesfully") % usermodel.get_username()) return True
def run(self): if self.__count > max_tries: show_error_dialog(_("Maximum number of tries reached, aborting.")) return None # References to password entries pe1 = self.__builder.get_object("password_entry_1") pe2 = self.__builder.get_object("password_entry_2") if self.__dialog.run() == 1: pw1 = pe1.get_text() pw2 = pe2.get_text() if (pw1 != pw2): show_error_dialog(_("Passwords do not match")) self.__dialog.destroy() return None else: self.__dialog.destroy() return pw1 else: self.__dialog.destroy() return None
def delete_user(self, menu_item = None): """Delete the selected user""" usermodel, t_iter = self.__get_selected_user() if t_iter is None: show_info_dialog(_("Select a user to delete!")) return # Users tend to delete many things they do not want # to delete if not ask_question(_("Really delete user <b>%s</b>?") % usermodel.get_username()): return # Delete user from ldap first try: self.__connection.delete_user(usermodel.get_dn()) except LumInsufficientPermissionsError: show_error_dialog(_("Insufficient permissions to delete user")) return None # Delete user from internal dictionary self.__user_store.remove(t_iter) self.statusbar_update(_("User %s deleted.") % usermodel.get_username())