def test_get_organization_admins(self): context_user = self._create_context() user = self._create_user("test_admins_1") context_user['user'] = "******" context_user_2 = self._create_context() user_2 = self._create_user("test_admins_2") context_user_2['user'] = "******" context_admin = self._create_context() organization = toolkit.get_action("organization_create")(context_admin, {"name": 'test_admins'}) for admin in get_organization_admins(organization['id']): self.assert_true(admin.id != user.id) member = toolkit.get_action("member_request_create")(context_user, {"group": organization['id'], 'role': "editor"}) toolkit.get_action("member_request_process")(context_admin, {"member": member['id'], "approve": False}) for admin in get_organization_admins(organization['id']): self.assert_true(admin.id != user.id) member = toolkit.get_action("member_request_create")(context_user, {"group": organization['id'], 'role': "editor"}) toolkit.get_action("member_request_process")(context_admin, {"member": member['id'], "approve": True}) for admin in get_organization_admins(organization['id']): self.assert_true(admin.id != user.id) member = toolkit.get_action("member_request_create")(context_user_2, {"group": organization['id'], 'role': "admin"}) toolkit.get_action("member_request_process")(context_admin, {"member": member['id'], "approve": True}) ok = False for admin in get_organization_admins(organization['id']): if admin.id == user_2.id: ok = True if not ok: self.fail("User not found from organization admins")
def _create_member_request(context, data_dict): """ Helper to create member request """ changed = False role = data_dict['role'] group = model.Group.get(data_dict['group']) if not group or group.type != 'organization': raise NotFound user = context['user'] if new_authz.is_sysadmin(user): raise ValidationError({}, {_("Role"): _("As a sysadmin, you already have access to all organizations")}) userobj = model.User.get(user) member = model.Session.query(model.Member).filter(model.Member.table_name == "user").filter(model.Member.table_id == userobj.id) \ .filter(model.Member.group_id == group.id).filter(or_(model.Member.state == 'active', model.Member.state == 'pending')).first() if member: if member.state == 'pending': message = _("You already have a pending request to the organization") else: message = _("You are already part of the organization") raise ValidationError({"organization": _("Duplicate organization request")}, {_("Organization"): message}) member = model.Session.query(model.Member).filter(model.Member.table_name == "user").filter(model.Member.table_id == userobj.id) \ .filter(model.Member.group_id == group.id).first() if not member: member = model.Member(table_name="user", table_id=userobj.id, group_id=group.id, capacity=role, state='pending') changed = True locale = _get_safe_locale() if member.state != 'pending' or changed: member.state = 'pending' member.capacity = role revision = model.repo.new_revision() revision.author = user if 'message' in context: revision.message = context['message'] elif changed: revision.message = u'New member request' else: revision.message = u'Changed member request' if changed: model.Session.add(member) else: member.save() extra = MemberExtra(member_id=member.id, key="locale", value=locale) extra.save() model.repo.commit() changed = True url = config.get('ckan.site_url', "") if url: url = url + url_for('member_request_show', member_id=member.id) if role == 'admin': for admin in get_ckan_admins(): _mail_new_membership_request(locale, admin, group.display_name, url, userobj.display_name, userobj.email) else: for admin in get_organization_admins(group.id): _mail_new_membership_request(locale, admin, group.display_name, url, userobj.display_name, userobj.email) return member, changed
def test_get_organization_admins(self): context_user = self._create_context() user = self._create_user("test_admins_1") context_user['user'] = "******" context_user_2 = self._create_context() user_2 = self._create_user("test_admins_2") context_user_2['user'] = "******" context_admin = self._create_context() organization = toolkit.get_action("organization_create")( context_admin, { "name": 'test_admins' }) for admin in get_organization_admins(organization['id']): self.assert_true(admin.id != user.id) member = toolkit.get_action("member_request_create")( context_user, { "group": organization['id'], 'role': "editor" }) toolkit.get_action("member_request_process")(context_admin, { "member": member['id'], "approve": False }) for admin in get_organization_admins(organization['id']): self.assert_true(admin.id != user.id) member = toolkit.get_action("member_request_create")( context_user, { "group": organization['id'], 'role': "editor" }) toolkit.get_action("member_request_process")(context_admin, { "member": member['id'], "approve": True }) for admin in get_organization_admins(organization['id']): self.assert_true(admin.id != user.id) member = toolkit.get_action("member_request_create")( context_user_2, { "group": organization['id'], 'role': "admin" }) toolkit.get_action("member_request_process")(context_admin, { "member": member['id'], "approve": True }) ok = False for admin in get_organization_admins(organization['id']): if admin.id == user_2.id: ok = True if not ok: self.fail("User not found from organization admins")
def _create_member_request(context, data_dict): """ Helper to create member request """ changed = False role = data_dict['role'] group = model.Group.get(data_dict['group']) if not group or group.type != 'organization': raise NotFound user = context['user'] if new_authz.is_sysadmin(user): raise ValidationError({}, { _("Role"): _("As a sysadmin, you already have access to all organizations") }) userobj = model.User.get(user) member = model.Session.query(model.Member).filter(model.Member.table_name == "user").filter( model.Member.table_id == userobj.id) \ .filter(model.Member.group_id == group.id).filter( or_(model.Member.state == 'active', model.Member.state == 'pending')).first() if member: if member.state == 'pending': message = _( "You already have a pending request to the organization") else: message = _("You are already part of the organization") raise ValidationError( {"organization": _("Duplicate organization request")}, {_("Organization"): message}) member = model.Session.query(model.Member).filter(model.Member.table_name == "user").filter( model.Member.table_id == userobj.id) \ .filter(model.Member.group_id == group.id).first() if not member: member = model.Member(table_name="user", table_id=userobj.id, group=group, group_id=group.id, capacity=role, state='pending') changed = True locale = _get_safe_locale() if member.state != 'pending' or changed: member.state = 'pending' member.capacity = role revision = model.repo.new_revision() revision.author = user if 'message' in context: revision.message = context['message'] elif changed: revision.message = u'New member request' else: revision.message = u'Changed member request' if changed: model.Session.add(member) else: member.save() extra = MemberExtra(member_id=member.id, key="locale", value=locale) extra.save() model.repo.commit() changed = True url = config.get('ckan.site_url', "") if url: url = url + url_for('member_request_show', member_id=member.id) admin_list = [] if role == 'admin': for admin in get_ckan_admins(): admin_list.append({ 'display_name': admin.display_name, 'email': admin.email }) # _mail_new_membership_request(locale, admin, group, url, userobj, data_dict) else: for admin in get_organization_admins(group.id): admin_list.append({ 'display_name': admin.display_name, 'email': admin.email }) # _mail_new_membership_request(locale, admin, group, url, userobj, data_dict) _mail_new_membership_request(locale, None, group, url, userobj, data_dict, admin_list) return member, changed
def _mail_process_status(locale, member_user, approve, group_name, capacity, group_id=None): try: if approve: subject = u'Approval of your request to join organisation ' + group_name email_data = { 'user_fullname': member_user.display_name, 'org_name': group_name, 'capacity': capacity } hdx_mailer.mail_recipient( [{ 'display_name': member_user.display_name, 'email': member_user.email }], subject, email_data, footer=member_user.email, snippet='email/content/join_organization_approval_to_user.html' ) admin_list = [] if group_id: for admin in get_organization_admins(group_id): if admin.email != member_user.email: admin_list.append({ 'display_name': admin.display_name, 'email': admin.email }) subject = u'Join request approved for organisation ' + group_name email_data = { 'user_fullname': member_user.display_name, 'user_email': member_user.email, 'org_name': group_name, } hdx_mailer.mail_recipient( admin_list, subject, email_data, footer=member_user.email, snippet= 'email/content/join_organization_approval_to_admins.html') else: subject = u'Denial of your request to join organisation ' + group_name email_data = { 'user_fullname': member_user.display_name, 'org_name': group_name, } hdx_mailer.mail_recipient( [{ 'display_name': member_user.display_name, 'email': member_user.email }], subject, email_data, footer=member_user.email, snippet='email/content/join_organization_reject_to_user.html') admin_list = [] if group_id: for admin in get_organization_admins(group_id): admin_list.append({ 'display_name': admin.display_name, 'email': admin.email }) subject = u'Join request denied for organisation ' + group_name email_data = { 'user_fullname': member_user.display_name, 'user_email': member_user.email, 'org_name': group_name, } hdx_mailer.mail_recipient( admin_list, subject, email_data, footer=member_user.email, snippet= 'email/content/join_organization_reject_to_admins.html') except MailerException, e: log.error(e)