Skip to content

Commit

Permalink
feat: add instance config endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff committed Aug 4, 2024
1 parent e413bfe commit 7cf2375
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 23 deletions.
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0

###> symfony/mailer ###
# MAILER_DSN=null://null
MAILER_SENDER_EMAIL=[email protected]
###< symfony/mailer ###

###> nelmio/cors-bundle ###
Expand All @@ -57,6 +56,10 @@ JWT_PASSPHRASE=827c9f8cce8bb82e75b2aec4a14a61f572ac28c7a8531f08dcdf1652573a7049
LOCK_DSN=flock
###< symfony/lock ###


MAILER_SENDER_EMAIL=[email protected]
LIMITED_FEATURES=false
OAUTH_ENABLED=false
OAUTH_CLIENT_ID=
OAUTH_CLIENT_SECRET=
OAUTH_AUTHORIZATION_URL=
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ Contributions are welcome as long as they do not contravene the Code of Conduct.

[^1]: RFC 3912 : WHOIS Protocol Specification. (2004). IETF Datatracker. https://datatracker.ietf.org/doc/html/rfc3912
[^2]: 2023 Global Amendments to the Base gTLD Registry Agreement (RA), Specification 13, and 2013 Registrar
Accreditation Agreement (RAA) - ICANN. (s. d.). https://www.icann.org/resources/pages/global-amendment-2023-en
Accreditation Agreement (RAA) - ICANN. (2023). https://www.icann.org/resources/pages/global-amendment-2023-en
2 changes: 1 addition & 1 deletion assets/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function App() {
if (location.pathname === '/login') navigate('/home')
}).catch(() => {
setIsAuthenticated(false)
navigate('/home')
if (location.pathname !== '/login') navigate('/home')
})
}, []);

Expand Down
10 changes: 6 additions & 4 deletions assets/pages/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {createContext, useContext, useEffect, useState} from "react";
import {Alert, Button, Card, Flex, Form, Input} from "antd";
import {getUser, login} from "../utils/api";
import {getConfiguration, getUser, InstanceConfig, login} from "../utils/api";
import {useNavigate} from "react-router-dom";
import {t} from 'ttag'

Expand All @@ -13,7 +13,8 @@ export const AuthenticatedContext = createContext<any>(null)

export default function LoginPage() {

const [error, setError] = useState()
const [error, setError] = useState<string>()
const [configuration, setConfiguration] = useState<InstanceConfig>()
const navigate = useNavigate()
const {setIsAuthenticated} = useContext(AuthenticatedContext)

Expand All @@ -32,6 +33,7 @@ export default function LoginPage() {
setIsAuthenticated(true)
navigate('/home')
})
getConfiguration().then(setConfiguration)
}, [])

return <Flex gap="middle" align="center" justify="center" vertical><Card
Expand Down Expand Up @@ -75,11 +77,11 @@ export default function LoginPage() {
{t`Submit`}
</Button>
</Form.Item>
<Form.Item wrapperCol={{offset: 8, span: 16}}>
{configuration?.ssoLogin && <Form.Item wrapperCol={{offset: 8, span: 16}}>
<Button type="primary" htmlType="button" href="/login/oauth">
{t`Log in with SSO`}
</Button>
</Form.Item>
</Form.Item>}
</Form>
</Card>
</Flex>
Expand Down
6 changes: 6 additions & 0 deletions assets/utils/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export interface Watchlist {
connector?: string
}

export interface InstanceConfig {
ssoLogin: boolean
limtedFeatures: boolean
}

export async function request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig): Promise<R> {
const axiosConfig: AxiosRequestConfig = {
...config,
Expand All @@ -84,6 +89,7 @@ export async function request<T = any, R = AxiosResponse<T>, D = any>(config: Ax
return await axios.request<T, R, D>(axiosConfig)
}


export * from './domain'
export * from './tld'
export * from './user'
Expand Down
9 changes: 8 additions & 1 deletion assets/utils/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {request, User} from "./index";
import {InstanceConfig, request, User} from "./index";


export async function login(email: string, password: string): Promise<boolean> {
Expand All @@ -16,3 +16,10 @@ export async function getUser(): Promise<User> {
})
return response.data
}

export async function getConfiguration(): Promise<InstanceConfig> {
const response = await request<InstanceConfig>({
url: 'config'
})
return response.data
}
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ security:
- { path: ^/api$, roles: PUBLIC_ACCESS }
- { path: ^/api/docs, roles: PUBLIC_ACCESS }
- { path: "^/api/watchlists/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/calendar$", roles: PUBLIC_ACCESS }
- { path: "^/api/config$", roles: PUBLIC_ACCESS }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }

when@test:
Expand Down
4 changes: 3 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
mailer_sender_email: '%env(MAILER_SENDER_EMAIL)%'
mailer_sender_email: '%env(string:MAILER_SENDER_EMAIL)%'
oauth_enabled: '%env(bool:OAUTH_ENABLED)%'
limited_features: '%env(bool:LIMITED_FEATURES)%'

services:
# default configuration for services in *this* file
Expand Down
20 changes: 20 additions & 0 deletions src/Controller/InstanceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Controller;

use App\Entity\Instance;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class InstanceController extends AbstractController
{
public function __invoke(): Instance
{
$instance = new Instance();

$instance
->setLimitedFeatures($this->getParameter('limited_features') ?? false)
->setOauthEnabled($this->getParameter('oauth_enabled'));

return $instance;
}
}
10 changes: 6 additions & 4 deletions src/Entity/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
name: 'get_all_mine',
),
new Get(
normalizationContext: ['groups' => 'connector:list']
normalizationContext: ['groups' => 'connector:list'],
security: 'object.user == user'
),
new Post(
routeName: 'connector_create', normalizationContext: ['groups' => ['connector:create', 'connector:list']],
denormalizationContext: ['groups' => 'connector:create'],
routeName: 'connector_create',
normalizationContext: ['groups' => ['connector:create', 'connector:list']], denormalizationContext: ['groups' => 'connector:create'],
security: 'object.user == user',
name: 'create'
),
new Delete(),
Expand All @@ -44,7 +46,7 @@ class Connector

#[ORM\ManyToOne(inversedBy: 'connectors')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
public ?User $user = null;

#[Groups(['connector:list', 'connector:create', 'watchlist:list'])]
#[ORM\Column(enumType: ConnectorProvider::class)]
Expand Down
48 changes: 48 additions & 0 deletions src/Entity/Instance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Controller\InstanceController;

#[ApiResource(
operations: [
new Get(
uriTemplate: '/config',
controller: InstanceController::class,
shortName: 'Configuration',
read: false,
),
]
)]
class Instance
{
private ?bool $oauthEnabled = null;

private ?bool $limitedFeatures = null;

public function isSsoLogin(): ?bool
{
return $this->oauthEnabled;
}

public function setOauthEnabled(bool $oauthEnabled): static
{
$this->oauthEnabled = $oauthEnabled;

return $this;
}

public function isLimitedFeatures(): ?bool
{
return $this->limitedFeatures;
}

public function setLimitedFeatures(bool $limitedFeatures): static
{
$this->limitedFeatures = $limitedFeatures;

return $this;
}
}
5 changes: 3 additions & 2 deletions src/Entity/WatchList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
name: 'get_all_mine',
),
new Get(
normalizationContext: ['groups' => 'watchlist:item']
normalizationContext: ['groups' => 'watchlist:item'],
security: 'object.user == user'
),
new Get(
routeName: 'watchlist_calendar',
Expand Down Expand Up @@ -73,7 +74,7 @@ class WatchList

#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'watchLists')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
public ?User $user = null;

/**
* @var Collection<int, Domain>
Expand Down
16 changes: 8 additions & 8 deletions translations/translations.pot
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ msgstr ""
#: assets/components/tracking/ConnectorForm.tsx:110
#: assets/components/tracking/ConnectorForm.tsx:119
#: assets/components/tracking/WatchlistForm.tsx:102
#: assets/pages/LoginPage.tsx:60
#: assets/pages/LoginPage.tsx:68
#: assets/pages/LoginPage.tsx:62
#: assets/pages/LoginPage.tsx:70
msgid "Required"
msgstr ""

Expand Down Expand Up @@ -319,7 +319,7 @@ msgid "Log out"
msgstr ""

#: assets/components/Sider.tsx:120
#: assets/pages/LoginPage.tsx:38
#: assets/pages/LoginPage.tsx:40
msgid "Log in"
msgstr ""

Expand Down Expand Up @@ -427,7 +427,7 @@ msgid ""
"their country of origin."
msgstr ""

#: assets/pages/LoginPage.tsx:58
#: assets/pages/LoginPage.tsx:60
#: assets/pages/watchdog/UserPage.tsx:18
msgid "Username"
msgstr ""
Expand Down Expand Up @@ -456,19 +456,19 @@ msgstr ""
msgid "Sorry, the page you visited does not exist."
msgstr ""

#: assets/pages/LoginPage.tsx:43
#: assets/pages/LoginPage.tsx:45
msgid "Error"
msgstr ""

#: assets/pages/LoginPage.tsx:66
#: assets/pages/LoginPage.tsx:68
msgid "Password"
msgstr ""

#: assets/pages/LoginPage.tsx:75
#: assets/pages/LoginPage.tsx:77
msgid "Submit"
msgstr ""

#: assets/pages/LoginPage.tsx:80
#: assets/pages/LoginPage.tsx:82
msgid "Log in with SSO"
msgstr ""

Expand Down

0 comments on commit 7cf2375

Please sign in to comment.