def _do_import_current_users(self, req, dry_run=False): """ """ active_users = [ user.username for user in UserManager(self.env).get_active_users() ] try: from acct_mgr.api import AccountManager known_users = list(AccountManager(self.env).get_users()) except: return [] imported_users = [] for username in known_users: if not username in active_users: imported_users.append(username) if not dry_run: UserManager(self.env).create_user(User(username)) if dry_run: return imported_users if len(imported_users) > 0: return _("Successfully imported the following users [%s].") % ( ','.join(imported_users)) else: return _("No users imported.")
def test_get_user(self): # create user UserManager(self.env).create_user(User(username='******', name="Catalin Balan", email='*****@*****.**')) # get user user = UserManager(self.env).get_user('cbalan') # test self.assertEqual(user['name'], 'Catalin Balan') self.assertEqual(user['email'], '*****@*****.**')
def test_search_users_1(self): # create users UserManager(self.env).create_user(User(username='******', name="Catalin Balan", email='*****@*****.**', company='opt')) UserManager(self.env).create_user(User(username='******', name="Andrei Culapov", email='*****@*****.**', company='ros')) UserManager(self.env).create_user(User(username='******', name="Andrei Culapov", email='*****@*****.**', company='ros')) # search users using "default_attributes" search_result = UserManager(self.env).search_users(User(email='%culapov%')) # test self.assertEqual(len(search_result), 1)
def test_search_users_2(self): # create users UserManager(self.env).create_user(User(username='******', name="Catalin Balan", email='*****@*****.**', company='opt')) UserManager(self.env).create_user(User(username='******', name="Andrei Culapov", email='*****@*****.**', company='ros')) UserManager(self.env).create_user(User(username='******', name="Andrei Culapov", email='*****@*****.**', company='ros')) # search users using "changes" user_template = User() user_template['email']='%culapov%' search_result = UserManager(self.env).search_users(user_template) # test self.assertEqual(len(search_result), 1)
def test_get_active_users(self): # create users UserManager(self.env).create_user(User(username='******', name="Catalin Balan", email='*****@*****.**')) UserManager(self.env).create_user(User(username='******', name="Andrei Culapov", email='*****@*****.**')) # get active users active_users = UserManager(self.env).get_active_users() # test self.assertEqual(active_users[0].username, 'cbalan') self.assertEqual(active_users[1].username, 'aculapov') self.assertEqual(len(active_users), 2)
def fill_user_details(self, username): """ Retrieves the user details from crowd and fills in session_attribute table """ user = UserManager(self.env).get_user(username) if not user['email'] or not user['name']: details = self.crowd.user_details(username) for detail in details: if detail.name == 'mail': user['email'] = detail.values[0] self.env.log.info(detail.values) if detail.name == 'sn': user['name'] = detail.values[0] if detail.name == 'givenName': user['name'] += " " + detail.values[0] user.save()
def test_create_user_1(self): # define user user = User() user.username='******' user['email']= '*****@*****.**' user['name']= 'Catalin Balan' # create user UserManager(self.env).create_user(user) # test db = self.env.get_db_cnx() cursor = db.cursor() cursor.execute("SELECT name, value " "FROM session_attribute " "WHERE sid='cbalan' and name in ('enabled', 'name', 'email') " "ORDER BY name") result = {} for name, value in cursor: result[name]=value self.assertEqual(result['name'], 'Catalin Balan') self.assertEqual(result['email'], '*****@*****.**') self.assertEqual(result['enabled'], '1') self.assertEqual(len(result),3)
def get_recipients(self, tktid): recipients = [] #self.env.log.warning('Sending to %s' % self.__team) for user in UserManager(self.env).get_active_users(): if user[self.__team] == '1': recipients.append(user['email']) #self.env.log.warning('Whhich is %s %s.' % (user.username, user['email'])) return (recipients, [])
def render_preference_panel(self, req, panel): """""" user = UserManager(self.env).get_user(req.session.sid) data = dict(messages=[], errors=[], user=user, um_profile_fields=UserProfileManager(self.env).get_user_profile_fields(ignore_internal=True)) if req.method=="POST": if req.args.has_key("um_profile_picture_remove"): if UserProfileManager(self.env).remove_user_file(user["picture_href"]): del user["picture_href"] if user.save(): data['messages'].append(_("Successfully removed %s's picture.")%(user.username)) req.redirect(req.href.prefs('userprofile')) return for field in data['um_profile_fields'].keys(): if req.args.has_key("um_profile_%s"%(field)): if data['um_profile_fields'][field]['type']=='file': user_file_new = UserProfileManager(self.env).get_uploaded_file_href(req, user, field, "um_profile_%s"%(field)) user_file_old = user[field] if user_file_new!=user_file_old: user[field] = user_file_new if user_file_old: try: UserProfileManager(self.env).remove_user_file(user_file_old) except Exception, e: self.log.error(e) data['errors'].append(_("Unable to remove previous %s=[%s]")%(field, user_file_old)) elif data['um_profile_fields'][field]['type']=='multichecks': user[field] = '|'.join(req.args.getlist("um_profile_%s"%(field))) else: user[field] = req.args.get("um_profile_%s"%(field)) elif data['um_profile_fields'][field]['type']=='multichecks': # cleanup if none selected user[field]=''
def test_save_user(self): # create user UserManager(self.env).create_user(User(username='******', name="Catalin Balan", email='*****@*****.**')) # get user user = UserManager(self.env).get_user('cbalan') user['bio']="Some bio..." user.save() user_reloaded = UserManager(self.env).get_user('cbalan') # test self.assertEqual(user_reloaded['name'], 'Catalin Balan') self.assertEqual(user_reloaded['email'], '*****@*****.**') self.assertEqual(user_reloaded['bio'], 'Some bio...')
def test_delete_attribute(self): # create users UserManager(self.env).create_user(User(username='******', name="Catalin Balan", email='*****@*****.**', company='opt')) UserManager(self.env).create_user(User(username='******', name="Andrei Culapov", email='*****@*****.**', company='ros')) UserManager(self.env).create_user(User(username='******', name="Andrei Culapov", email='*****@*****.**', company='ros')) # test before db = self.env.get_db_cnx() cursor = db.cursor() cursor.execute("SELECT name, value " "FROM session_attribute " "WHERE sid='cbalan' " "ORDER BY name") result = {} for name, value in cursor: result[name]=value self.assertEqual(len(result),4) # delete attribute user=UserManager(self.env).get_user('cbalan') del user['name'] del user['email'] user.save() # test after db = self.env.get_db_cnx() cursor = db.cursor() cursor.execute("SELECT name, value " "FROM session_attribute " "WHERE sid='cbalan' " "ORDER BY name") result = {} for name, value in cursor: result[name]=value self.assertEqual(len(result),2)
def _do_delete_user(self, req): """ """ if UserManager(self.env).delete_user( req.args.get('um_deleteuser_username')): return _("Successfully removed user [%s].") % ( req.args.get('um_deleteuser_username'))
provider = providers.get(panel) try: panel_template, data = provider.render_usermanager_admin_panel( req, panel, user, panel_path_info) um_data.update(template=panel_template) except Exception, e: um_data['errors'].append(e) # Moving messages from data to um_data if data.has_key('errors'): um_data['errors'].extend(data.pop('errors')) if data.has_key('messages'): um_data['messages'].extend(data.pop('messages')) # adding user list um_data.update(users=UserManager(self.env).get_active_users()) # additional cells um_data.update(cells=list(self._get_cells(um_data['users']))) # adding usernamager's data to the data dict data.update(user_manager=um_data) data['sort'] = req.args.get('sort') or 'name' data['asc'] = req.args.get('asc') or '1' # checking for external users trac_managed_users_out = self._do_import_current_users(req, dry_run=True) if len(trac_managed_users_out) > 0: um_data['errors'].append(
def render_admin_panel(self, req, category, page, path_info): assert req.perm.has_permission('TICKET_ADMIN') action = req.args.get('action') users = UserManager(self.env).get_active_users() caption = self.getCaption() teams = self.getTeams() if action: # load data from post if action == 'rename': caption = req.args.get('caption') self.setCaption(caption) elif action == 'add': newTeam = req.args.get('newTeam') canAdd = True for team in teams: if team == newTeam: canAdd = False break if canAdd: teams.append(newTeam) for user in users: user[newTeam] = '0' user.save() self.setTeams(teams) elif action == 'up': id = req.args.get('id') i = teams.index(id) if i > 0: tmp = teams[i - 1] teams[i - 1] = teams[i] teams[i] = tmp self.setTeams(teams) elif action == 'down': id = req.args.get('id') i = teams.index(id) if i < len(teams) - 1: tmp = teams[i + 1] teams[i + 1] = teams[i] teams[i] = tmp self.setTeams(teams) elif action == 'delete': id = req.args.get('id') teams.remove(id) for user in users: del user[id] user.save() self.setTeams(teams) elif action == 'updateUsers': for user in users: for team in teams: if req.args.get('%s_%s' % (user.username, team)): user[team] = '1' else: user[team] = '0' user.save() return 'ttd_admin.html', { 'teams': teams, 'users': users, 'caption': caption }
def get_src(self, req, username, size): user_manager = UserManager(self.env) user = user_manager.get_user(username) if not user or not user['picture_href']: return req.href.chrome('userpictures/default-portrait.gif') return user['picture_href']
class UserManagementAdminPage(Component): implements(IAdminPanelProvider) panel_providers = ExtensionPoint(IUserManagerPanelProvider) cells_providers = ExtensionPoint(IUserListCellContributor) default_panel = Option('user_manager', 'admin_default_panel', 'profile', """Default user admin panel.""") # IAdminPageProvider methods def get_admin_panels(self, req): if req.perm.has_permission('TRAC_ADMIN'): yield ('accounts', _('Accounts'), 'users', 'Users') def render_admin_panel(self, req, cat, page, path_info): username = None panel = None panel_path_info = None data = {} um_data = dict(default_panel=self.default_panel, messages=[], errors=[]) # collecting username, current panel and eventual path_info if path_info is not None: path_info_list = path_info.split('/') username = path_info_list[0] if len(path_info_list) > 1: panel = path_info_list[1] if len(path_info_list) > 2: panel_path_info = path_info_list[2:] # action handling if req.args.has_key('um_session_management') and panel is None: return self._do_session_management(req, cat, page, path_info) if req.method == "POST" and panel is None: try: if req.args.has_key("um_newuser_create"): um_data['messages'].append(self._do_create_user(req)) elif req.args.has_key("um_user_delete"): um_data['messages'].append(self._do_delete_user(req)) elif req.args.has_key('um_import_current_users'): um_data['messages'].append( self._do_import_current_users(req)) except Exception, e: um_data['errors'].append(e) if username: user = UserManager(self.env).get_user(username) panels, providers = self._get_panels(req) um_data.update(user=user, panels=panels) if panel: um_data['default_panel'] = panel if providers.has_key(panel): um_data.update(panel=panel) provider = providers.get(panel) try: panel_template, data = provider.render_usermanager_admin_panel( req, panel, user, panel_path_info) um_data.update(template=panel_template) except Exception, e: um_data['errors'].append(e) # Moving messages from data to um_data if data.has_key('errors'): um_data['errors'].extend(data.pop('errors')) if data.has_key('messages'): um_data['messages'].extend(data.pop('messages'))
def expand_macro(self, formatter, name, content): env = self.env req = formatter.req content_args = {} data = dict(user_profiles=[], user_profile_fields={}) layout_args = {} rendered_result = "" user_profile_templates = [] # collecting arguments if content: for i, macro_args in enumerate(content.split('|')): if i == 0: content_args = MacroArguments(macro_args) continue if i == 1: layout_args = MacroArguments(macro_args) break # extracting userProfile attrs if len(content_args) > 0: user_profile_templates.append(User(**content_args)) if len(content_args.get_list_args()) > 0: for list_item in content_args.get_list_args(): user_profile_templates.append( User(**MacroArguments(list_item[1:len(list_item) - 1]))) # adding profiles fields description data['user_profile_fields'].update( UserProfileManager(env).get_user_profile_fields( ignore_internal=True)) # removing picture_href data['user_profile_fields'].pop('picture_href') def inline_wiki_to_html(text): return wiki_to_html(text, env, req) data['wiki_to_html'] = inline_wiki_to_html # grabbing users if len(user_profile_templates) > 0: data['user_profiles'] = UserManager(env).search_users( user_profile_templates) else: data['user_profiles'] = UserManager(env).get_active_users() data['cells'] = list(self._get_cells(req, data['user_profiles'])) # add stylesheet&script add_script(req, 'tracusermanager/js/macros_um_profile.js') add_stylesheet(req, 'tracusermanager/css/macros_um_profile.css') # render template template = Chrome(env).load_template('macro_um_profile.html', method='xhtml') data = Chrome(env).populate_data(req, {'users': data}) rendered_result = template.generate(**data) # wrap everything if len(layout_args) > 0: rendered_result = html.div(rendered_result, **layout_args) return rendered_result