コード例 #1
0
def _data_company_save():
    contact_id = request.json["contact"]
    contact = Account.find_account_by_id(contact_id)
    if not contact:
        raise P2k16UserException("No such account: {}".format(contact_id))

    _id = request.json.get("id", None)

    if _id:
        company = Company.find_by_id(_id)

        if company is None:
            abort(404)

        logger.info("Updating company: {}".format(company))
        company.name = request.json["name"]
        company.contact_id = contact.id
        company.active = request.json["active"]
    else:
        name = request.json["name"]
        active = request.json["active"]
        logger.info("Registering new company: {}".format(name))
        company = Company(name, contact, active)

    db.session.add(company)
    db.session.commit()

    return jsonify(company_to_json(company, include_employees=True))
コード例 #2
0
def data_company(company_id: int):
    company = Company.find_by_id(company_id)

    if company is None:
        abort(404)

    return jsonify(company_to_json(company, include_employees=True))
コード例 #3
0
ファイル: authz_management.py プロジェクト: bitraf/p2k16
def can_haz_door_access(account, doors=[]):

    # If called with no doors, this basically checks if you are paying member
    # or employed at a company. It is used to show the buttons on the front page
    # for now, in the future, we probably want to make those dependant on access,
    # so you only see buttons you have access to.

    # Employed people have access to all doors for now
    if Company.is_account_employed(account.id):
        return True

    # If you aren't a paying member, you have access to no doors
    # Check paying membership
    if not StripePayment.is_account_paying_member(account.id):
        return False

    # Find all circles the account is a member of, then iterate the doors and check
    memberships = {
        circle.name
        for circle in account_management.get_circles_for_account(account.id)
    }

    for door in doors:
        if not memberships & door.circles:
            # No overlap, so we lack access to this door, lets return false
            return False

    # If we get here, we had access to all doors, so return True
    return True
コード例 #4
0
def profile_to_json(account: Account,
                    circles: List[Circle],
                    badges: Optional[List[AccountBadge]],
                    full=False):
    from .badge_blueprint import badge_to_json
    json = {
        "account": account_to_json(account),
        "avatar": create_avatar_url(account.email or account.username),
        "circles": {c.id: {
            "id": c.id,
            "name": c.name
        }
                    for c in circles},
        "badges": {b.id: badge_to_json(b)
                   for b in badges} if badges else None,
    }

    if full:
        json["active_member"] = active_member(account)
        json["membership_fee"] = get_membership_fee(account)
        json["has_door_access"] = authz_management.can_haz_door_access(account)
        json["is_paying_member"] = StripePayment.is_account_paying_member(
            account.id)
        json["is_employed"] = Company.is_account_employed(account.id)

    return json
コード例 #5
0
def _data_company_change_employee(company_id, add: bool):
    check_is_in_circle("admin")

    company = Company.find_by_id(company_id)

    if company is None:
        abort(404)

    account_id = request.json["account_id"]
    account = Account.find_account_by_id(account_id)
    if account is None:
        abort(404)

    if add:
        db.session.add(CompanyEmployee(company, account))
    else:
        ce = CompanyEmployee.find_by_company_and_account(
            company_id, account_id)
        if ce is None:
            abort(404)
        db.session.delete(ce)

    employees = CompanyEmployee.list_by_company(company_id)
    db.session.commit()

    return jsonify(company_to_json(company, employees))
コード例 #6
0
def data_company(company_id: int):
    company = Company.find_by_id(company_id)

    if company is None:
        abort(404)

    employees = CompanyEmployee.list_by_company(company_id)

    return jsonify(company_to_json(company, employees))
コード例 #7
0
def active_member(account: Account = None) -> bool:
    """
    Verify that user is an active member of Bitraf either by paying or member of company
    """

    # Check paying membership
    if StripePayment.is_account_paying_member(account.id):
        return True

    # Check company membership
    if Company.is_account_employed(account.id):
        return True

    return False
コード例 #8
0
    def open_doors(self, account: Account, doors: List[Door]):
        door_circle = Circle.get_by_name('door')

        can_open_door = False

        if account_management.is_account_in_circle(
                account,
                door_circle) and membership_management.active_member(account):
            can_open_door = True

        if len(Company.find_active_companies_with_account(account.id)) > 0:
            can_open_door = True

        if not can_open_door:
            # Only non-office users may fail here
            if not membership_management.active_member(account):
                raise P2k16UserException(
                    '{} does not have an active membership'.format(
                        account.display_name()))

            if not account_management.is_account_in_circle(
                    account, door_circle):
                raise P2k16UserException('{} is not in the door circle'.format(
                    account.display_name()))

        publishes = []

        if not event_management.has_opened_door(account):
            system = Account.find_account_by_username("system")
            logger.info("First door opening for {}".format(account))
            badge_management.create_badge(account, system,
                                          "first-door-opening")

        for door in doors:
            logger.info(
                'Opening door. username={}, door={}, open_time={}'.format(
                    account.username, door.key, door.open_time))
            event_management.save_event(OpenDoorEvent(door.key))
            publishes.append((self.prefix + door.topic, str(door.open_time)))

        # Make sure everything has been written to the database before actually opening the door.
        db.session.flush()

        # TODO: move this to a handler that runs after the transaction is done
        # TODO: we can look at the responses and see if they where successfully sent/received.
        for topic, open_time in publishes:
            logger.info("Sending message: {}: {}".format(topic, open_time))
            self._client.publish(topic, open_time)
コード例 #9
0
def active_member(account: Account = None) -> bool:
    """
    Verify that user is an active member of Bitraf either by paying or member of company
    """

    # Check paying membership
    if StripePayment.query. \
        filter(StripePayment.created_by_id == account.id,
               StripePayment.end_date >= (datetime.utcnow() - timedelta(days=1))).count() > 0:
        return True

    # Check company membership
    if len(Company.find_active_companies_with_account(account.id)) > 0:
        return True

    return False
コード例 #10
0
    def checkout_tool(self, account: Account, tool: ToolDescription):
        # Check that user has correct circle and is paying member
        if not account_management.is_account_in_circle(account, tool.circle):
            raise P2k16UserException('{} is not in the {} circle'.format(
                account.display_name(), tool.circle.name))

        if not membership_management.active_member(account) \
                and len(Company.find_active_companies_with_account(account.id)) == 0:
            raise P2k16UserException(
                '{} does not have an active membership and is not employed in an active company'
                .format(account.display_name()))

        logger.info('Checking out tool. username={}, tool={}'.format(
            account.username, tool.name))

        # Verify that tool is not checked out by someone else. Check in first if it is.
        checkout = ToolCheckout.find_by_tool(tool)
        if checkout:
            if checkout.account is account:
                raise P2k16UserException('Tools can only be checked out once.')

            logger.info(
                'Tool checked out by someone else. Assuming control: username={}, tool={}, old_username={}'
                .format(account.username, tool.name, checkout.account.name))
            self.checkin_tool(checkout.account, checkout.tool_description)

        # Make a new checkout reservation
        event_management.save_event(
            ToolCheckoutEvent(tool.name, datetime.now(), account))
        checkout = ToolCheckout(tool, account, datetime.now())
        db.session.add(checkout)

        # Make sure everything has been written to the database before actually opening the door.
        db.session.flush()

        # TODO: move this to a handler that runs after the transaction is done
        # TODO: we can look at the responses and see if they where successfully sent/received.
        #        for topic, open_time in publishes:
        #            logger.info("Sending message: {}: {}".format(topic, open_time))
        #            self._client.publish(topic, open_time)

        topic = self._mqtt_topic(tool=tool.name, action='unlock')
        payload = 'true'
        logger.info("Sending message: {}: {}".format(topic, payload))
        self._client.publish(topic, payload)
コード例 #11
0
def _data_company_change_employee(company_id, add: bool):
    company = Company.get_by_id(company_id)

    account_id = request.json["accountId"]
    account = Account.find_account_by_id(account_id)
    if account is None:
        abort(404)

    if add:
        db.session.add(CompanyEmployee(company, account))
    else:
        ce = CompanyEmployee.find_by_company_and_account(company_id, account_id)
        if ce is None:
            abort(404)
        db.session.delete(ce)

    db.session.commit()

    return jsonify(company_to_json(company, include_employees=True))