Ejemplo n.º 1
0
 def _getInvitations(self):
     L = []
     for invitation in self.context.values():
         if IInvitation.providedBy(invitation):
             L.append(invitation)
     return L
Ejemplo n.º 2
0
    def _add_user_csv_row(self, search, profiles, users, row, reactivate, i):
        errors = []
        messages = []

        username = row.pop('username')
        login = row.pop('login', username)
        if not username:
            errors.append(
                "Malformed CSV: line %d has an empty username." %
                (i+2))

        email = row['email']
        if not email:
            errors.append(
                'Malformed CSV: line %d has an empty email address.' % (i+2)
            )

        if errors:
            return 0, messages, errors

        website = row.pop('website', None)
        if website is not None:
            row['websites'] = website.strip().split()

        profile = profiles.get(username)
        skip = False
        if (users.get_by_id(username) is not None or
            (profile is not None and profile.security_state != 'inactive')):
            messages.append(
                "Skipping user: %s: User already exists." %
                username
            )
            skip = True
        elif users.get_by_login(login):
            messages.append(
                "Skipping user: %s: User already exists with "
                "login: %s" % (username, login)
            )
            skip = True

        if skip:
            return 0, messages, errors

        merge_profile = None
        count, docids, resolver = search(email=email)
        if count > 1:
            errors.append(
                'Multiple users already exist with email '
                'address: %s' % email
            )
        elif count == 1:
            previous = resolver(iter(docids).next())
            if IInvitation.providedBy(previous):
                # This user was previously invited to join a community.  Remove
                # the invitation and carry on
                del previous.__parent__[previous.__name__]
            else:
                merge_profile = resolver(docids[0])
                if merge_profile.security_state != 'inactive':
                    errors.append(
                        'An active user already exists with email '
                        'address: %s.' % email
                    )
                elif not reactivate:
                    errors.append(
                        'A previously deactivated user exists with '
                        'email address: %s.  Consider checking the '
                        '"Reactivate user" checkbox to reactivate '
                        'the user.' % email
                    )

                if merge_profile.__name__ == username:
                    merge_profile = None

        if profile is None:
            profile = profiles.get(username)
            if profile is not None and not reactivate:
                errors.append(
                    'A previously deactivated user exists with username: %s.  '
                    'Consider checking the "Reactivate user" checkbox to '
                    'reactivate the user.' % username
                )

        if errors:
            return 0, messages, errors

        groups = row.pop('groups', '')
        groups = set(groups.split())
        if 'sha_password' in row:
            users.add(username, login, row.pop('sha_password'),
                      groups, encrypted=True)
        else:
            users.add(username, login, row.pop('password'),
                      groups)
        decoded = {}
        for k, v in row.items():
            if isinstance(v, str):
                try:
                    v = v.decode('utf8')
                except UnicodeDecodeError:
                    v = v.decode('latin1')
            decoded[k] = v
        if profile is None:
            profile = create_content(IProfile, **decoded)
            profiles[username] = profile
            workflow = get_workflow(IProfile, 'security', profile)
            if workflow is not None:
                workflow.initialize(profile)
        else:
            messages.append('Reactivated %s.' % username)
            for k, v in decoded.items():
                setattr(profile, k, v)
            workflow = get_workflow(IProfile, 'security', profile)
            workflow.transition_to_state(profile, None, 'active')

        if merge_profile is not None:
            merge_messages = StringIO()
            self.rename_user(profile, merge_profile.__name__, username,
                             merge=True, out=merge_messages)
            messages += merge_messages.getvalue().split('\n')

        return 1, messages, errors
Ejemplo n.º 3
0
Archivo: members.py Proyecto: zagy/karl
 def _getInvitations(self):
     L = []
     for invitation in self.context.values():
         if IInvitation.providedBy(invitation):
             L.append(invitation)
     return L
Ejemplo n.º 4
0
Archivo: admin.py Proyecto: zagy/karl
    def _add_user_csv_row(self, search, profiles, users, row, reactivate, i):
        errors = []
        messages = []

        username = row.pop('username')
        login = row.pop('login', username)
        if not username:
            errors.append("Malformed CSV: line %d has an empty username." %
                          (i + 2))

        email = row['email']
        if not email:
            errors.append(
                'Malformed CSV: line %d has an empty email address.' % (i + 2))

        if errors:
            return 0, messages, errors

        website = row.pop('website', None)
        if website is not None:
            row['websites'] = website.strip().split()

        profile = profiles.get(username)
        skip = False
        if (users.get_by_id(username) is not None or
            (profile is not None and profile.security_state != 'inactive')):
            messages.append("Skipping user: %s: User already exists." %
                            username)
            skip = True
        elif users.get_by_login(login):
            messages.append("Skipping user: %s: User already exists with "
                            "login: %s" % (username, login))
            skip = True

        if skip:
            return 0, messages, errors

        merge_profile = None
        count, docids, resolver = search(email=email)
        if count > 1:
            errors.append('Multiple users already exist with email '
                          'address: %s' % email)
        elif count == 1:
            previous = resolver(iter(docids).next())
            if IInvitation.providedBy(previous):
                # This user was previously invited to join a community.  Remove
                # the invitation and carry on
                del previous.__parent__[previous.__name__]
            else:
                merge_profile = resolver(docids[0])
                if merge_profile.security_state != 'inactive':
                    errors.append('An active user already exists with email '
                                  'address: %s.' % email)
                elif not reactivate:
                    errors.append('A previously deactivated user exists with '
                                  'email address: %s.  Consider checking the '
                                  '"Reactivate user" checkbox to reactivate '
                                  'the user.' % email)

                if merge_profile.__name__ == username:
                    merge_profile = None

        if profile is None:
            profile = profiles.get(username)
            if profile is not None and not reactivate:
                errors.append(
                    'A previously deactivated user exists with username: %s.  '
                    'Consider checking the "Reactivate user" checkbox to '
                    'reactivate the user.' % username)

        if errors:
            return 0, messages, errors

        groups = row.pop('groups', '')
        groups = set(groups.split())
        if 'sha_password' in row:
            users.add(username,
                      login,
                      row.pop('sha_password'),
                      groups,
                      encrypted=True)
        else:
            users.add(username, login, row.pop('password'), groups)
        decoded = {}
        for k, v in row.items():
            if isinstance(v, str):
                try:
                    v = v.decode('utf8')
                except UnicodeDecodeError:
                    v = v.decode('latin1')
            decoded[k] = v
        if profile is None:
            profile = create_content(IProfile, **decoded)
            profiles[username] = profile
            workflow = get_workflow(IProfile, 'security', profile)
            if workflow is not None:
                workflow.initialize(profile)
        else:
            messages.append('Reactivated %s.' % username)
            for k, v in decoded.items():
                setattr(profile, k, v)
            workflow = get_workflow(IProfile, 'security', profile)
            workflow.transition_to_state(profile, None, 'active')

        if merge_profile is not None:
            merge_messages = StringIO()
            self.rename_user(profile,
                             merge_profile.__name__,
                             username,
                             merge=True,
                             out=merge_messages)
            messages += merge_messages.getvalue().split('\n')

        return 1, messages, errors