Ejemplo n.º 1
0
    def test_delete_by_id(
        self,
        create_common_resolvers: Callable,
    ) -> None:

        username = "******"
        database_context = LinOTP_DatabaseContext(SqlSession=db.session,
                                                  SqlEngine=db.engine)

        ih = SQLImportHandler(
            groupid="LinOTP_local_admins",
            resolver_name="LinOTP_local_admins",
            database_context=database_context,
        )

        session = database_context.get_session()
        userids = [user.userid for user in session.query(ImportedUser).all()]

        assert username in userids
        ih.delete_by_id(username)

        session.commit()

        userids = [user.userid for user in session.query(ImportedUser).all()]

        assert username not in userids
Ejemplo n.º 2
0
    def import_users(self):
        """
        import users from a csv file into an dedicated sql resolver
        """

        try:

            params = {}
            params.update(request.POST)

            # -------------------------------------------------------------- --
            # processing required arguments
            try:

                data_file = request.POST['file']
                resolver_name = params['resolver']

            except KeyError as exx:

                log.exception("Missing parameter: %r", exx)
                raise ParameterError("Missing parameter: %r" % exx)

            groupid = resolver_name

            # process file upload data

            data = data_file

            # -- ----------------------------------------------------------- --
            # In case of form post requests, it is a "instance" of FieldStorage
            # i.e. the Filename is selected in the browser and the data is
            # transferred in an iframe.
            #     see: http://jquery.malsup.com/form/#sample4
            # -- ----------------------------------------------------------- --

            if isinstance(data_file, FieldStorage):
                data = data_file.value

            # -------------------------------------------------------------- --

            # process the other arguments
            dryrun = boolean(params.get('dryrun', False))

            passwords_in_plaintext = boolean(
                params.get('passwords_in_plaintext', False))

            file_format = params.get('format', "csv")

            if file_format in ('password', 'passwd'):

                column_mapping = {
                    "userid": 2,
                    "username": 0,
                    "phone": 8,
                    "mobile": 7,
                    "email": 9,
                    "surname": 5,
                    "givenname": 4,
                    "password": 1
                }

                format_reader = PasswdFormatReader()

            elif file_format in ('csv'):

                skip_header = boolean(params.get('skip_header', False))
                if skip_header:
                    data = '\n'.join(data.split('\n')[1:])

                column_mapping = {
                    "username": 0,
                    "userid": 1,
                    "surname": 2,
                    "givenname": 3,
                    "email": 4,
                    "phone": 5,
                    "mobile": 6,
                    "password": 7
                }

                delimiter = str(params.get('delimiter', ","))
                quotechar = str(params.get('quotechar', '"'))

                format_reader = DefaultFormatReader()
                format_reader.delimiter = delimiter
                format_reader.quotechar = quotechar

                column_mapping = params.get('column_mapping', column_mapping)

            else:

                raise Exception('unspecified file foramt')

            # we have to convert the column_mapping back into an dict

            if (isinstance(column_mapping, str)
                    or isinstance(column_mapping, unicode)):
                column_mapping = json.loads(column_mapping)

            # prevent overwrite of existing unmanaged resolver

            resolvers = getResolverList()
            if resolver_name in resolvers:
                if not resolvers[resolver_name].get('readonly', False):
                    raise Exception("Unmanged resolver with same name: %r"
                                    " already exists!" % resolver_name)
            # -------------------------------------------------------------- --

            # feed the engine :)

            # use a LinOTP Database context for Sessions and Engine

            db_context = LinOTP_DatabaseContext(
                SqlSession=Session, SqlEngine=linotp.model.meta.engine)

            # define the import into an SQL database + resolver

            import_handler = SQLImportHandler(groupid=groupid,
                                              resolver_name=resolver_name,
                                              database_context=db_context)

            # create the UserImporter with the required mapping

            user_import = UserImport(import_handler)

            user_import.set_mapping(column_mapping)

            # and run the data processing

            result = user_import.import_csv_users(
                data,
                dryrun=dryrun,
                format_reader=format_reader,
                passwords_in_plaintext=passwords_in_plaintext)

            if dryrun:

                return sendResult(response, result)

            # -------------------------------------------------------------- --

            # create / extend target realm for the resolver

            resolver_spec = import_handler.get_resolver_spec()

            Session.commit()

            return sendResult(response, result)

        except PolicyException as pexx:

            log.exception("Error during user import: %r", pexx)

            Session.rollback()

            return sendError(response, "%r" % pexx, 1)

        except Exception as exx:

            log.exception("Error during user import: %r" % exx)

            Session.rollback()

            return sendError(response, "%r" % exx)

        finally:
            Session.close()
            log.debug('done')
Ejemplo n.º 3
0
    def import_users(self):
        """
        import users from a csv file into an dedicated sql resolver
        """

        try:

            params = self.request_params

            # -------------------------------------------------------------- --
            # processing required arguments
            try:

                data_file = request.files["file"]
                resolver_name = params["resolver"]

            except KeyError as exx:

                log.error("Missing parameter: %r", exx)
                raise ParameterError("Missing parameter: %r" % exx)

            if resolver_name == current_app.config["ADMIN_RESOLVER_NAME"]:
                raise DeleteForbiddenError(
                    f"default admin resolver {resolver_name} is not allowed "
                    "to be overwritten!")

            groupid = resolver_name

            # process file upload data

            data = data_file

            # -- ----------------------------------------------------------- --
            # In case of form post requests, it is a "instance" of FileStorage
            # i.e. the Filename is selected in the browser and the data is
            # transferred in an iframe.
            #     see: http://jquery.malsup.com/form/#sample4
            # -- ----------------------------------------------------------- --

            if isinstance(data_file, FileStorage):
                data = data_file.read()

            data = data.decode()

            # -------------------------------------------------------------- --

            # process the other arguments
            dryrun = boolean(params.get("dryrun", False))

            passwords_in_plaintext = boolean(
                params.get("passwords_in_plaintext", False))

            file_format = params.get("format", "csv")

            if file_format in ("password", "passwd"):

                column_mapping = {
                    "userid": 2,
                    "username": 0,
                    "phone": 8,
                    "mobile": 7,
                    "email": 9,
                    "surname": 5,
                    "givenname": 4,
                    "password": 1,
                }

                format_reader = PasswdFormatReader()

            elif file_format in ("csv"):

                skip_header = boolean(params.get("skip_header", False))
                if skip_header:
                    data = "\n".join(data.split("\n")[1:])

                column_mapping = {
                    "username": 0,
                    "userid": 1,
                    "surname": 2,
                    "givenname": 3,
                    "email": 4,
                    "phone": 5,
                    "mobile": 6,
                    "password": 7,
                }

                delimiter = str(params.get("delimiter", ","))
                quotechar = str(params.get("quotechar", '"'))

                format_reader = DefaultFormatReader()
                format_reader.delimiter = delimiter
                format_reader.quotechar = quotechar

                column_mapping = params.get("column_mapping", column_mapping)

            else:

                raise Exception("unspecified file foramt")

            # we have to convert the column_mapping back into an dict

            if isinstance(column_mapping, str):
                column_mapping = json.loads(column_mapping)

            # prevent overwrite of existing unmanaged resolver

            checkPolicyPre("system", "setResolver")

            resolvers = getResolverList()
            if resolver_name in resolvers:
                if not resolvers[resolver_name].get("readonly", False):
                    raise Exception("Unmanged resolver with same name: %r"
                                    " already exists!" % resolver_name)
            # -------------------------------------------------------------- --

            # feed the engine :)

            # use a LinOTP Database context for Sessions and Engine

            db_context = LinOTP_DatabaseContext(SqlSession=db.session,
                                                SqlEngine=db.engine)

            # define the import into an SQL database + resolver

            import_handler = SQLImportHandler(
                groupid=groupid,
                resolver_name=resolver_name,
                database_context=db_context,
            )

            # create the UserImporter with the required mapping

            user_import = UserImport(import_handler)

            user_import.set_mapping(column_mapping)

            # and run the data processing

            result = user_import.import_csv_users(
                data,
                dryrun=dryrun,
                format_reader=format_reader,
                passwords_in_plaintext=passwords_in_plaintext,
            )

            if dryrun:

                return sendResult(response, result)

            # -------------------------------------------------------------- --

            # create / extend target realm for the resolver

            resolver_spec = import_handler.get_resolver_spec()

            db.session.commit()

            return sendResult(response, result)

        except PolicyException as pexx:

            log.error("Error during user import: %r", pexx)

            db.session.rollback()

            return sendError(response, "%r" % pexx, 1)

        except Exception as exx:

            log.error("Error during user import: %r", exx)

            db.session.rollback()

            return sendError(response, exx)

        finally:
            log.debug("done")