Пример #1
0
def create_user(**kwargs):
    """Create user in Caliopen instance."""
    from caliopen_main.user.core import User
    from caliopen_main.user.parameters import NewUser
    from caliopen_main.contact.parameters import NewContact
    from caliopen_main.contact.parameters import NewEmail

    # Fill core registry
    from caliopen_main.message.objects.message import Message

    param = NewUser()
    param.name = kwargs['email']
    if '@' in param.name:
        username, domain = param.name.split('@')
        param.name = username
        # Monkey patch configuration local_domain with provided value
        conf = Configuration('global').configuration
        conf['default_domain'] = domain
    param.password = kwargs['password']
    param.recovery_email = u'{}@caliopen.local'.format(param.name)

    contact = NewContact()
    contact.given_name = kwargs.get('given_name')
    contact.family_name = kwargs.get('family_name')
    email = NewEmail()
    email.address = param.recovery_email
    contact.emails = [email]
    param.contact = contact
    user = User.create(param)
    log.info('User %s (%s) created' % (user.user_id, user.name))
Пример #2
0
def create_user(**kwargs):
    """Create user in Caliopen instance."""
    from caliopen_main.user.core import User
    from caliopen_main.user.parameters import NewUser
    from caliopen_main.contact.parameters import NewContact
    from caliopen_main.contact.parameters import NewEmail

    # Fill core registry
    from caliopen_main.message.objects.message import Message

    param = NewUser()
    param.name = kwargs['email']
    if '@' in param.name:
        username, domain = param.name.split('@')
        param.name = username
        # Monkey patch configuration local_domain with provided value
        conf = Configuration('global').configuration
        conf['default_domain'] = domain
    param.password = kwargs['password']
    param.recovery_email = u'{}@recovery-caliopen.local'.format(param.name)

    contact = NewContact()
    contact.given_name = kwargs.get('given_name')
    contact.family_name = kwargs.get('family_name')
    email = NewEmail()
    email.address = param.recovery_email
    contact.emails = [email]
    param.contact = contact
    user = User.create(param)
    log.info('User %s (%s) created' % (user.user_id, user.name))
Пример #3
0
    def collection_post(self):
        """Create a new user."""
        contact = self.request.swagger_data['user']['contact']
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        settings.validate()
        param = NewUser({'name': self.request.swagger_data['user']['username'],
                         'password': self.request.swagger_data['user'][
                             'password'],
                         'recovery_email': self.request.swagger_data['user'][
                             'recovery_email'],
                         'contact': contact,
                         'settings': settings,
                         })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            raise AuthenticationError(exc.message)  # why this class of error ?

        log.info('Created user {} with name {}'.
                 format(user.user_id, user.name))
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')
        return {'location': user_url}
Пример #4
0
    def collection_post(self):
        """Create a new user."""
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        try:
            settings.validate()
        except Exception as exc:
            raise Unprocessable(detail=exc.message)

        param = NewUser({
            'name':
            self.request.swagger_data['user']['username'],
            'password':
            self.request.swagger_data['user']['password'],
            'recovery_email':
            self.request.swagger_data['user']['recovery_email'],
            'settings':
            settings,
        })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            c.family_name = ""  # can't guess it !
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            log.exception('Error during user creation {0}'.format(exc))
            raise NotAcceptable(detail=exc.message)

        log.info('Created user {} with name {}'.format(user.user_id,
                                                       user.name))
        # default device management
        in_device = self.request.swagger_data['user']['device']
        if in_device:
            try:
                in_device['name'] = 'default'
                device = Device.create_from_parameter(user, in_device,
                                                      self.request.headers)
                log.info('Device %r created' % device.device_id)
            except Exception as exc:
                log.exception('Error during default device creation %r' % exc)
        else:
            log.warn('Missing default device parameter')
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')
        return {'location': user_url}
Пример #5
0
    def collection_post(self):
        """Create a new user."""
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        try:
            settings.validate()
        except Exception as exc:
            raise Unprocessable(detail=exc.message)

        param = NewUser({
            'name':
            self.request.swagger_data['user']['username'],
            'password':
            self.request.swagger_data['user']['password'],
            'recovery_email':
            self.request.swagger_data['user']['recovery_email'],
            'settings':
            settings,
        })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            c.family_name = ""  # can't guess it !
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            raise NotAcceptable(detail=exc.message)

        log.info('Created user {} with name {}'.format(user.user_id,
                                                       user.name))
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')
        return {'location': user_url}
Пример #6
0
    def collection_post(self):
        """Create a new user."""
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        try:
            settings.validate()
        except Exception as exc:
            raise Unprocessable(detail=exc.message)

        param = NewUser({'name': self.request.swagger_data['user']['username'],
                         'password': self.request.swagger_data['user'][
                             'password'],
                         'recovery_email': self.request.swagger_data['user'][
                             'recovery_email'],
                         'settings': settings,
                         })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            c.family_name = ""  # can't guess it !
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            log.exception('Error during user creation {0}'.format(exc))
            raise NotAcceptable(detail=exc.message)

        log.info('Created user {} with name {}'.
                 format(user.user_id, user.name))
        # default device management
        in_device = self.request.swagger_data['user']['device']
        if in_device:
            try:
                in_device['name'] = 'default'
                device = Device.create_from_parameter(user, in_device,
                                                      self.request.headers)
                log.info('Device %r created' % device.device_id)
            except Exception as exc:
                log.exception('Error during default device creation %r' % exc)
        else:
            log.warn('Missing default device parameter')
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')

        # send notification to apiv2 to trigger post-registration actions
        config = Configuration('global').get("message_queue")
        try:
            tornado.ioloop.IOLoop.current().run_sync(
                lambda: notify_new_user(user, config), timeout=5)
        except Exception as exc:
            log.exception(
                'Error when sending new_user notification on NATS : {0}'.
                    format(exc))

        return {'location': user_url}