def delete(self, ctx, id_: int): try: if ctx.get(CTX_ADMIN) != self.device_manager.get_owner( ctx, id_) and Roles.ADMIN_WRITE.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") self.main_manager.delete(ctx, id=id_) return NoContent, 204 # 204 No Content except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def vendor_search(self, ctx, id_: int): """ Return the vendor associated with the given device """ try: device = self.device_manager.get_by_id(ctx=ctx, id=id_) if ctx.get( CTX_ADMIN ) != device.member and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.device_manager.get_mac_vendor(ctx, id=id_), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def member_get(self, ctx, id_: int): try: if ctx.get(CTX_ADMIN) != id_ and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.room_manager.room_from_member(ctx, id_), 200 except Exception as e: return handle_error(ctx, e)
def member_put(self, ctx, id_: int, body: Dict[str, int]): try: if ctx.get(CTX_ADMIN) != id_ and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") self.mailinglist_manager.update_member_mailinglist(ctx, id_, body['value']) return NoContent, 204 except Exception as e: return handle_error(ctx, e)
def member_get(self, ctx, charter_id: int, id_: int): try: if ctx.get(CTX_ADMIN ) != id_ and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.charter_manager.get(ctx, charter_id, id_), 200 except Exception as e: return handle_error(ctx, e)
def profile(self, ctx): try: member, roles = self.member_manager.get_profile(ctx) if member.id != ctx.get(CTX_ADMIN): raise UnauthorizedError( "Not authorize to access this ressource") return {"member": member.to_dict(), "roles": roles}, 200 except Exception as e: return handle_error(ctx, e)
def post(self, ctx, body: dict = {}): try: device_body = DeviceBody.from_dict(body) if ctx.get( CTX_ADMIN ) != device_body.member and Roles.ADMIN_WRITE.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.device_manager.create(ctx, device_body).id, 201 except Exception as e: return handle_error(ctx, e)
def statuses_search(self, ctx, id_: int): try: return list( map(lambda x: x.to_dict(), self.member_manager.get_statuses(ctx, id_))), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def charter_put(self, ctx, id_, charter_id) -> Tuple[Any, int]: try: if ctx.get(CTX_ADMIN ) != id_ and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") self.charter_manager.sign(ctx, charter_id=charter_id, member_id=id_) return NoContent, 204 except Exception as e: return handle_error(ctx, e)
def add_membership_payment_record(self, ctx, membership: Membership, free: bool): LOG.debug("membership_add_membership_payment_record", extra=log_extra(ctx, duration=membership.duration, membership_accoun=membership.account)) if free and not Roles.TRESO_WRITE.value in ctx.get(CTX_ROLES): raise UnauthorizedError( "Impossibilité de faire une cotisation gratuite") payment_method = self.payment_method_repository.get_by_id( ctx, membership.payment_method) asso_account, _ = self.account_repository.search_by( ctx, limit=1, filter_=AbstractAccount( name=KnownAccountExpense.ASSOCIATION_EXPENCE.value)) if len(asso_account) != 1: raise AccountNotFoundError( KnownAccountExpense.ASSOCIATION_EXPENCE.value) tech_account, _ = self.account_repository.search_by( ctx, limit=1, filter_=AbstractAccount( name=KnownAccountExpense.TECHNICAL_EXPENSE.value)) if len(tech_account) != 1: raise AccountNotFoundError( KnownAccountExpense.TECHNICAL_EXPENSE.value) src_account = self.account_repository.get_by_id( ctx, membership.account) price = self.duration_price[membership.duration] # Expressed in EUR. if price == 50 and not membership.has_room: price = 9 duration_str = self.duration_string.get(membership.duration) title = f'Internet - {duration_str}' self.transaction_repository.create( ctx, AbstractTransaction(value=9 if not free else 0, src=src_account.id, dst=asso_account[0].id, name=title + " (gratuit)" if free else title, payment_method=payment_method.id)) if price > 9 and not free: self.transaction_repository.create( ctx, AbstractTransaction(value=price - 9, src=src_account.id, dst=tech_account[0].id, name=title, payment_method=payment_method.id))
def password_put(self, ctx, id_, body): """ Set the password of a member. """ try: self.member_manager.change_password(ctx, id_, body.get('password'), body.get("hashedPassword")) return NoContent, 204 # 204 No Content except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def get(self, ctx, id_: int, only: t.Optional[t.List[str]] = None): try: device = self.device_manager.get_by_id(ctx, id=id_) if ctx.get( CTX_ADMIN ) != device.member and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") def remove(entity: t.Any) -> t.Any: if isinstance(entity, dict) and only is not None: entity_cp = entity.copy() for k in entity_cp.keys(): if k not in only + ["id"]: del entity[k] return entity return remove(device.to_dict()), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def get(self, ctx, id_: int, only: Optional[List[str]] = None): try: def remove(entity: Any) -> Any: if isinstance(entity, dict) and only is not None: entity_cp = entity.copy() for k in entity_cp.keys(): if k not in only + ["id"]: del entity[k] return entity return remove(self.main_manager.get_by_id(ctx, id=id_).to_dict()), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, filter_: dict = {}): try: device_filter = DeviceFilter.from_dict(filter_) if ctx.get( CTX_ADMIN ) != device_filter.member and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") result, total_count = self.device_manager.search( ctx, limit=limit, offset=offset, device_filter=device_filter) headers = { "X-Total-Count": str(total_count), 'access-control-expose-headers': 'X-Total-Count' } return result, 200, headers except Exception as e: return handle_error(ctx, e)
def update_port_vlan(self, ctx, port_id: int, vlan: int = 1) -> str: """ Update the VLAN assigned to a port. :raise PortNotFound """ oid, ip, community = self.get_oid_switch_ipand_community_from_port_id( ctx, port_id) try: roles = ctx.get(CTX_ROLES) vlan = int(vlan) if (Roles.NETWORK_WRITE.value not in roles and ( ((vlan == 3 or vlan == 103) and Roles.NETWORK_DEV.value not in roles) or ((vlan == 2 or vlan == 102) and Roles.NETWORK_PROD.value not in roles) or ((vlan == 104) and Roles.NETWORK_HOSTING.value not in roles))): raise UnauthorizedError() return set_SNMP_value(community, ip, 'CISCO-VLAN-MEMBERSHIP-MIB', 'vmVlan', oid, vlan) except Exception as e: raise e