예제 #1
0
    def activate(self, **kwargs):
        if request.method != "POST":
            return O.none()
        if not kwargs:
            kwargs = request.json
        username = kwargs['user']
        key = kwargs['code']
        user = request.db.query(User).join(Org, ApiKey).filter(
            User.username == username, ApiKey.value == key,
            ApiKey.enabled == True).first()  # noqa
        if not user:
            user = request.db.query(User).join(Org, ApiKey).filter(
                User.username == username,
                User.enabled == True).first()  # noqa
            if user:
                return O.error(msg="Already enabled")
            else:
                return O.error(msg="User not found")

        user.enabled = True
        api_key = request.db.query(ApiKey).filter(ApiKey.value == key).one()
        new_key = ApiKey(user=user)

        request.db.delete(api_key)
        request.db.add(user)
        request.db.add(new_key)
예제 #2
0
    def activate(self, **kwargs):
        if request.method != "POST":
            return O.none()
        if not kwargs:
            kwargs = request.json
        username = kwargs['user']
        key = kwargs['code']
        user = request.db.query(User).join(Org, ApiKey).filter(
            User.username == username,
            ApiKey.value == key, ApiKey.enabled == True).first()  # noqa
        if not user:
            user = request.db.query(User).join(Org, ApiKey).filter(
                User.username == username,
                User.enabled == True).first()  # noqa
            if user:
                return O.error(msg="Already enabled")
            else:
                return O.error(msg="User not found")

        user.enabled = True
        api_key = request.db.query(ApiKey).filter(ApiKey.value == key).one()
        new_key = ApiKey(user=user)

        request.db.delete(api_key)
        request.db.add(user)
        request.db.add(new_key)
예제 #3
0
    def deployments(self, name=None, **kwargs):
        skip = ['id', 'owner_id']

        def _encode(s):
            try:
                return json.loads(s)
            except ValueError:
                return {}
        if name:
            depl = Deployment.my(request).filter(
                Deployment.name == name).first()
            if depl:
                return O.deployment(**depl.serialize(
                    skip=skip,
                    rel=[('content', 'content',
                          lambda p: _encode(p))]))
            else:
                return O.error(msg="Cannot find deployment '%s'" % name)
        else:
            depl = sorted([d.serialize(skip=skip)
                           for d in Deployment.my(request).all()],
                          key=lambda d: d['name'])
            return O._anon(deployments=depl,
                           quota=dict(allowed=request.user.tier.deployments))
        return O.none()
예제 #4
0
    def create(self, name, **kwargs):
        if request.method != "POST":
            return O.none()
        name = name or kwargs['name']

        try:
            user = User.visible(request).filter(
                User.id == request.user.id).first()
            content = kwargs.pop('content')
            if not isinstance(content, dict):
                content = json.loads(content)

            _validate(content)

        except KeyError, kerr:
            return O.error(msg="Missing content data: %s" % kerr)
예제 #5
0
    def reset(self, email=None, **kwargs):
        if request.method != "POST":
            return O.none()
        if not email:
            email = request.json.get("email")
        user = request.db.query(User).filter(User.email == email).first()
        if not user:
            return O.success(msg="Message sent to the specified email.")
        token = User.create_token(request, user.id, scope='RECOVER')

        ACTION_URL = "%s/index.html#page/recovery/%s" % (
            conf.DASH_SERVER_URL.rstrip('/'), token.value)
        html = render('email/recover.html', dict(ACTION_URL=ACTION_URL))
        requests.post("https://api.mailgun.net/v2/cloudrunner.io/messages",
                      auth=("api", "key-276qmsiyxi8z5tvie2bvxm2jhfxkhjh9"),
                      data={
                          "from":
                          "CloudRunner.IO Team <*****@*****.**>",
                          "to": [email],
                          "subject": "[CloudRunner.IO] Recover lost password",
                          "html": html
                      })
예제 #6
0
    def reset(self, email=None, **kwargs):
        if request.method != "POST":
            return O.none()
        if not email:
            email = request.json.get("email")
        user = request.db.query(User).filter(
            User.email == email).first()
        if not user:
            return O.success(msg="Message sent to the specified email.")
        token = User.create_token(request, user.id, scope='RECOVER')

        ACTION_URL = "%s/index.html#page/recovery/%s" % (
            conf.DASH_SERVER_URL.rstrip('/'),
            token.value)
        html = render('email/recover.html',
                      dict(ACTION_URL=ACTION_URL))
        requests.post(
            "https://api.mailgun.net/v2/cloudrunner.io/messages",
            auth=("api", "key-276qmsiyxi8z5tvie2bvxm2jhfxkhjh9"),
            data={"from": "CloudRunner.IO Team <*****@*****.**>",
                  "to": [email],
                  "subject": "[CloudRunner.IO] Recover lost password",
                  "html": html})
예제 #7
0
    def register(self, **kwargs):
        if request.method != "POST":
            return O.none()
        if not kwargs:
            kwargs = request.json

        plan_id = kwargs["plan_id"]
        username = kwargs["username"]
        password = kwargs["password"]
        email = kwargs["email"]

        # Attach cloudrunner-library repo
        check_existing = request.db.query(Repository).filter(
            Repository.type == 'github',
            Repository.org_id == None,
            Repository.name == CR_LIBRARY).first()  # noqa
        if check_existing:
            repository = check_existing
        else:
            repository = Repository(name=CR_LIBRARY, private=False,
                                    type='github')
            request.db.add(repository)
            root = Folder(name="/", full_name="/", repository=repository)
            request.db.add(root)

        plan = request.db.query(UsageTier).filter(
            UsageTier.name == plan_id).one()
        org = Org(name="ORG-%s" % username, tier=plan, enabled=True)
        user = User(username=username, email=email, org=org,
                    first_name=kwargs["first_name"],
                    last_name=kwargs["last_name"],
                    department=kwargs.get("department"),
                    position=kwargs.get("position"),
                    enabled=False)
        user.set_password(password)

        key = ApiKey(user=user)
        adm_role = Role(servers="*", as_user="******")
        perm = Permission(name="is_admin", user=user)
        group = Group(org=org, name="Default")
        group.users.append(user)
        group.roles.append(adm_role)

        request.db.add(org)
        request.db.add(user)
        request.db.add(group)
        request.db.add(adm_role)
        request.db.add(perm)
        request.db.add(key)

        repository_link = Repository(name=CR_LIBRARY, private=False,
                                     type='github',
                                     owner=user,
                                     org=org)
        repository_link.linked = repository
        request.db.add(repository_link)

        auth_user = '******'
        auth_pass = ''
        auth_args = ''
        creds = RepositoryCreds(provider='github', auth_user=auth_user,
                                auth_pass=auth_pass, auth_args=auth_args,
                                repository=repository_link)
        request.db.add(creds)

        try:
            request.db.commit()
        except IntegrityError, ierr:
            if hasattr(ierr, 'orig'):
                LOG.error(ierr.orig)
            else:
                LOG.error(vars(ierr))
            request.db.rollback()
            try_find = DUPL_SEARCH2.findall(str(ierr))
            if try_find and len(try_find[0]) == 2:
                if try_find[0][0] == "name":
                    return O.error(
                        msg=ERR_REGISTER_USERNAME, reason="duplicate")
                elif try_find[0][0] == "email":
                    return O.error(
                        msg=ERR_REGISTER_EMAIL, reason="duplicate")
                else:
                    return O.error(msg=ERR_REGISTER_UNKNOWN,
                                   reason="duplicate")
            elif "uq_organizations_name" in str(ierr):
                return O.error(
                    msg=ERR_REGISTER_USERNAME, reason="duplicate")
            elif "uq_users_email" in str(ierr):
                return O.error(
                    msg=ERR_REGISTER_EMAIL, reason="duplicate")
            else:
                return O.error(msg=ERR_REGISTER_UNKNOWN, reason="duplicate")
예제 #8
0
    def register(self, **kwargs):
        if request.method != "POST":
            return O.none()
        if not kwargs:
            kwargs = request.json

        plan_id = kwargs["plan_id"]
        username = kwargs["username"]
        password = kwargs["password"]
        email = kwargs["email"]

        # Attach cloudrunner-library repo
        check_existing = request.db.query(Repository).filter(
            Repository.type == 'github', Repository.org_id == None,
            Repository.name == CR_LIBRARY).first()  # noqa
        if check_existing:
            repository = check_existing
        else:
            repository = Repository(name=CR_LIBRARY,
                                    private=False,
                                    type='github')
            request.db.add(repository)
            root = Folder(name="/", full_name="/", repository=repository)
            request.db.add(root)

        plan = request.db.query(UsageTier).filter(
            UsageTier.name == plan_id).one()
        org = Org(name="ORG-%s" % username, tier=plan, enabled=True)
        user = User(username=username,
                    email=email,
                    org=org,
                    first_name=kwargs["first_name"],
                    last_name=kwargs["last_name"],
                    department=kwargs.get("department"),
                    position=kwargs.get("position"),
                    enabled=False)
        user.set_password(password)

        key = ApiKey(user=user)
        adm_role = Role(servers="*", as_user="******")
        perm = Permission(name="is_admin", user=user)
        group = Group(org=org, name="Default")
        group.users.append(user)
        group.roles.append(adm_role)

        request.db.add(org)
        request.db.add(user)
        request.db.add(group)
        request.db.add(adm_role)
        request.db.add(perm)
        request.db.add(key)

        repository_link = Repository(name=CR_LIBRARY,
                                     private=False,
                                     type='github',
                                     owner=user,
                                     org=org)
        repository_link.linked = repository
        request.db.add(repository_link)

        auth_user = '******'
        auth_pass = ''
        auth_args = ''
        creds = RepositoryCreds(provider='github',
                                auth_user=auth_user,
                                auth_pass=auth_pass,
                                auth_args=auth_args,
                                repository=repository_link)
        request.db.add(creds)

        try:
            request.db.commit()
        except IntegrityError, ierr:
            if hasattr(ierr, 'orig'):
                LOG.error(ierr.orig)
            else:
                LOG.error(vars(ierr))
            request.db.rollback()
            try_find = DUPL_SEARCH2.findall(str(ierr))
            if try_find and len(try_find[0]) == 2:
                if try_find[0][0] == "name":
                    return O.error(msg=ERR_REGISTER_USERNAME,
                                   reason="duplicate")
                elif try_find[0][0] == "email":
                    return O.error(msg=ERR_REGISTER_EMAIL, reason="duplicate")
                else:
                    return O.error(msg=ERR_REGISTER_UNKNOWN,
                                   reason="duplicate")
            elif "uq_organizations_name" in str(ierr):
                return O.error(msg=ERR_REGISTER_USERNAME, reason="duplicate")
            elif "uq_users_email" in str(ierr):
                return O.error(msg=ERR_REGISTER_EMAIL, reason="duplicate")
            else:
                return O.error(msg=ERR_REGISTER_UNKNOWN, reason="duplicate")