I rightly do not know what this all is

This commit is contained in:
Michel 2024-10-30 19:40:26 +01:00
parent af6b2b752e
commit 287c1f67c5
78 changed files with 3484 additions and 3365 deletions

254
.gitignore vendored
View file

@ -1,127 +1,127 @@
# -----------------------------------------------------------------
# .gitignore
# Bare Minimum Git
# https://salferrarello.com/starter-gitignore-file/
# ver 20221125
#
# From the root of your project run
# curl -O https://gist.githubusercontent.com/salcode/10017553/raw/.gitignore
# to download this file
#
# This file is tailored for a general web project, it
# is NOT optimized for a WordPress project. See
# https://gist.github.com/salcode/b515f520d3f8207ecd04
# for a WordPress specific .gitignore
#
# This file specifies intentionally untracked files to ignore
# http://git-scm.com/docs/gitignore
#
# NOTES:
# The purpose of gitignore files is to ensure that certain files not
# tracked by Git remain untracked.
#
# To ignore uncommitted changes in a file that is already tracked,
# use `git update-index --assume-unchanged`.
#
# To stop tracking a file that is currently tracked,
# use `git rm --cached`
#
# Change Log:
# 20221125 ignore /dist directory
# unignore /.git-blame-ignore-revs
# 20220720 ignore /build directory
# 20220128 unignore .nvmrc
# 20210211 unignore .env.example
# 20190705 ignore private/secret files
# 20181206 remove trailing whitespaces
# 20180714 unignore .phpcs.xml.dist
# 20170502 unignore composer.lock
# 20170502 ignore components loaded via Bower
# 20150326 ignore jekyll build directory `/_site`
# 20150324 Reorganized file to list ignores first and whitelisted last,
# change WordPress .gitignore link to preferred gist,
# add curl line for quick installation
# ignore composer files (vendor directory and lock file)
# 20140606 Add .editorconfig as a tracked file
# 20140418 remove explicit inclusion
# of readme.md (this is not an ignored file by default)
# 20140407 Initially Published
#
# -----------------------------------------------------------------
# ignore all files starting with . or ~
.*
~*
# ignore node/grunt dependency directories
node_modules/
# Ignore build directories.
/build
/dist
# ignore composer vendor directory
/vendor
# ignore components loaded via Bower
/bower_components
# ignore jekyll build directory
/_site
# ignore OS generated files
ehthumbs.db
Thumbs.db
# ignore Editor files
*.sublime-project
*.sublime-workspace
*.komodoproject
# ignore log files and databases
*.log
*.sql
*.sqlite
# ignore compiled files
*.com
*.class
*.dll
*.exe
*.o
*.so
# ignore packaged files
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
# ignore private/secret files
*.der
*.key
*.pem
# --------------------------------------------------------
# BEGIN Explictly Allowed Files (i.e. do NOT ignore these)
# --------------------------------------------------------
# track these files, if they exist
!.editorconfig
!.env.example
!.git-blame-ignore-revs
!.gitignore
!.nvmrc
!.phpcs.xml.dist
# ------------
# USER CHANGES
# ------------
# ignore public directory (we build it on server)
/public
/data/
# -----------------------------------------------------------------
# .gitignore
# Bare Minimum Git
# https://salferrarello.com/starter-gitignore-file/
# ver 20221125
#
# From the root of your project run
# curl -O https://gist.githubusercontent.com/salcode/10017553/raw/.gitignore
# to download this file
#
# This file is tailored for a general web project, it
# is NOT optimized for a WordPress project. See
# https://gist.github.com/salcode/b515f520d3f8207ecd04
# for a WordPress specific .gitignore
#
# This file specifies intentionally untracked files to ignore
# http://git-scm.com/docs/gitignore
#
# NOTES:
# The purpose of gitignore files is to ensure that certain files not
# tracked by Git remain untracked.
#
# To ignore uncommitted changes in a file that is already tracked,
# use `git update-index --assume-unchanged`.
#
# To stop tracking a file that is currently tracked,
# use `git rm --cached`
#
# Change Log:
# 20221125 ignore /dist directory
# unignore /.git-blame-ignore-revs
# 20220720 ignore /build directory
# 20220128 unignore .nvmrc
# 20210211 unignore .env.example
# 20190705 ignore private/secret files
# 20181206 remove trailing whitespaces
# 20180714 unignore .phpcs.xml.dist
# 20170502 unignore composer.lock
# 20170502 ignore components loaded via Bower
# 20150326 ignore jekyll build directory `/_site`
# 20150324 Reorganized file to list ignores first and whitelisted last,
# change WordPress .gitignore link to preferred gist,
# add curl line for quick installation
# ignore composer files (vendor directory and lock file)
# 20140606 Add .editorconfig as a tracked file
# 20140418 remove explicit inclusion
# of readme.md (this is not an ignored file by default)
# 20140407 Initially Published
#
# -----------------------------------------------------------------
# ignore all files starting with . or ~
.*
~*
# ignore node/grunt dependency directories
node_modules/
# Ignore build directories.
/build
/dist
# ignore composer vendor directory
/vendor
# ignore components loaded via Bower
/bower_components
# ignore jekyll build directory
/_site
# ignore OS generated files
ehthumbs.db
Thumbs.db
# ignore Editor files
*.sublime-project
*.sublime-workspace
*.komodoproject
# ignore log files and databases
*.log
*.sql
*.sqlite
# ignore compiled files
*.com
*.class
*.dll
*.exe
*.o
*.so
# ignore packaged files
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
# ignore private/secret files
*.der
*.key
*.pem
# --------------------------------------------------------
# BEGIN Explictly Allowed Files (i.e. do NOT ignore these)
# --------------------------------------------------------
# track these files, if they exist
!.editorconfig
!.env.example
!.git-blame-ignore-revs
!.gitignore
!.nvmrc
!.phpcs.xml.dist
# ------------
# USER CHANGES
# ------------
# ignore public directory (we build it on server)
/public
/data/

View file

@ -18,6 +18,7 @@
"symfony/uid": "^7.1",
"php-curl-class/php-curl-class": "^9.19",
"symfony/cache": "^7.1",
"phpoffice/phpspreadsheet": "^2.1"
"phpoffice/phpspreadsheet": "^2.1",
"symfony/polyfill-iconv": "^1.31"
}
}

452
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,3 @@
@import "@fortawesome/fontawesome-free/scss/fontawesome";
@import "@fortawesome/fontawesome-free/scss/solid";
@import "@fortawesome/fontawesome-free/scss/fontawesome";
@import "@fortawesome/fontawesome-free/scss/solid";
@import "@fortawesome/fontawesome-free/scss/brands";

View file

@ -1,44 +1,44 @@
@import "bootstrap";
@import "fonts";
$main-container-max-width: 992px;
.cursor-pointer {
cursor: pointer;
}
.navigation-container,
main {
width: 100%;
max-width: $main-container-max-width;
margin: 0 auto;
}
.ratio-1 {
aspect-ratio: 1;
}
.avatar {
height: 3rem;
.avatar-login-method-icon {
scale: 1.5;
}
}
@include media-breakpoint-down(lg) {
.mode-switch {
text-align: center;
padding-top: 0.5rem;
margin-left: auto;
}
.navigation-container {
.navbar-brand {
margin: 0 auto !important;
}
}
@import "bootstrap";
@import "fonts";
$main-container-max-width: 992px;
.cursor-pointer {
cursor: pointer;
}
.navigation-container,
main {
width: 100%;
max-width: $main-container-max-width;
margin: 0 auto;
}
.ratio-1 {
aspect-ratio: 1;
}
.avatar {
height: 3rem;
.avatar-login-method-icon {
scale: 1.5;
}
}
@include media-breakpoint-down(lg) {
.mode-switch {
text-align: center;
padding-top: 0.5rem;
margin-left: auto;
}
.navigation-container {
.navbar-brand {
margin: 0 auto !important;
}
}
}

View file

@ -1,3 +1,3 @@
import "./theme";
import "./theme";
import "bootstrap/js/src/collapse";

View file

@ -1,39 +1,39 @@
(() => {
function setTheme (mode = 'auto') {
const userMode = localStorage.getItem('bs-theme');
const sysMode = window.matchMedia('(prefers-color-scheme: light)').matches;
const useSystem = mode === 'system' || (!userMode && mode === 'auto');
const modeChosen = selectMode(mode);
if (useSystem) {
localStorage.removeItem('bs-theme');
} else {
localStorage.setItem('bs-theme', modeChosen);
}
document.documentElement.setAttribute('data-bs-theme', useSystem ? (sysMode ? 'light' : 'dark') : modeChosen);
document.querySelectorAll('.mode-switch .btn').forEach(e => e.classList.remove('text-body'));
document.getElementById(modeChosen)?.classList.add('text-body');
}
function selectMode(mode: string): string {
const userMode = <string>localStorage.getItem('bs-theme');
const useSystem = mode === 'system' || (!userMode && mode === 'auto');
if (useSystem) {
return 'system';
}
if (mode === 'dark' || mode === 'light') {
return mode;
}
return userMode;
}
document.addEventListener('DOMContentLoaded', () => {
setTheme();
document.querySelectorAll('.mode-switch .btn').forEach(e => e.addEventListener('click', () => setTheme(e.id)));
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', () => setTheme());
});
(() => {
function setTheme (mode = 'auto') {
const userMode = localStorage.getItem('bs-theme');
const sysMode = window.matchMedia('(prefers-color-scheme: light)').matches;
const useSystem = mode === 'system' || (!userMode && mode === 'auto');
const modeChosen = selectMode(mode);
if (useSystem) {
localStorage.removeItem('bs-theme');
} else {
localStorage.setItem('bs-theme', modeChosen);
}
document.documentElement.setAttribute('data-bs-theme', useSystem ? (sysMode ? 'light' : 'dark') : modeChosen);
document.querySelectorAll('.mode-switch .btn').forEach(e => e.classList.remove('text-body'));
document.getElementById(modeChosen)?.classList.add('text-body');
}
function selectMode(mode: string): string {
const userMode = <string>localStorage.getItem('bs-theme');
const useSystem = mode === 'system' || (!userMode && mode === 'auto');
if (useSystem) {
return 'system';
}
if (mode === 'dark' || mode === 'light') {
return mode;
}
return userMode;
}
document.addEventListener('DOMContentLoaded', () => {
setTheme();
document.querySelectorAll('.mode-switch .btn').forEach(e => e.addEventListener('click', () => setTheme(e.id)));
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', () => setTheme());
});
})()

View file

@ -1,102 +1,102 @@
import '../../../css/common/index.scss';
import 'datatables.net-bs5/css/dataTables.bootstrap5.css';
import '../../common/index';
import DataTable from 'datatables.net-bs5';
import {Modal} from "bootstrap";
const DT_SERVERSIDE_PROCESSING_URL = '/api/dt/accounts';
const TABLE = document.querySelector('#user-table');
function displayEdit(data: any) {
const modalElem = document.querySelector('#edit-modal');
// @ts-ignore
modalElem.querySelector('.name-input').textContent = data.name;
// @ts-ignore
modalElem.querySelector('.login-method').textContent = data.loginMethod;
// @ts-ignore
modalElem.querySelector('.permission-editor').value = data.permissionIndex;
// @ts-ignore
modalElem?.querySelector('.js--save').dataset.userid = data.userid;
Modal.getOrCreateInstance('#edit-modal').show();
}
document.addEventListener('DOMContentLoaded', () => {
const dt = new DataTable('#user-table', {
ajax: {
url: DT_SERVERSIDE_PROCESSING_URL,
},
serverSide: true,
columns: [
{
data: 'profilePictureUrl',
render(data, type) {
if (type !== 'display') {
return data;
}
return `<img src="${ data }" alt="Profile Picture" class="ratio-1 rounded-circle w-100" />`
},
orderable: false,
searchable: false
},
{
name: 'Name',
data: 'name'
},
{
name: 'Permission',
data: 'permission'
},
{
name: 'Login-Method',
data: 'loginMethod'
}
],
order: [ [1, 'asc'] ],
drawCallback: function (settings) {
const api = new DataTable.Api(settings);
api.rows().every(function (row) {
const node = this.node();
const data = this.data();
node.addEventListener('click', (e) => {
displayEdit(data);
});
});
}
});
const button = <HTMLButtonElement>document.querySelector('#edit-modal .js--save');
if (!button) {
return;
}
button.addEventListener('click', async (e) => {
const permissionEditor = <HTMLSelectElement>document.querySelector('#edit-modal .permission-editor');
if (!permissionEditor) {
return;
}
const formData = new FormData();
formData.set('permission', permissionEditor.value);
const response = await fetch(
`/api/web/users/${button.dataset.userid}`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
Modal.getOrCreateInstance('#edit-modal').hide();
dt.draw();
})
import '../../../css/common/index.scss';
import 'datatables.net-bs5/css/dataTables.bootstrap5.css';
import '../../common/index';
import DataTable from 'datatables.net-bs5';
import {Modal} from "bootstrap";
const DT_SERVERSIDE_PROCESSING_URL = '/api/dt/accounts';
const TABLE = document.querySelector('#user-table');
function displayEdit(data: any) {
const modalElem = document.querySelector('#edit-modal');
// @ts-ignore
modalElem.querySelector('.name-input').textContent = data.name;
// @ts-ignore
modalElem.querySelector('.login-method').textContent = data.loginMethod;
// @ts-ignore
modalElem.querySelector('.permission-editor').value = data.permissionIndex;
// @ts-ignore
modalElem?.querySelector('.js--save').dataset.userid = data.userid;
Modal.getOrCreateInstance('#edit-modal').show();
}
document.addEventListener('DOMContentLoaded', () => {
const dt = new DataTable('#user-table', {
ajax: {
url: DT_SERVERSIDE_PROCESSING_URL,
},
serverSide: true,
columns: [
{
data: 'profilePictureUrl',
render(data, type) {
if (type !== 'display') {
return data;
}
return `<img src="${ data }" alt="Profile Picture" class="ratio-1 rounded-circle w-100" />`
},
orderable: false,
searchable: false
},
{
name: 'Name',
data: 'name'
},
{
name: 'Permission',
data: 'permission'
},
{
name: 'Login-Method',
data: 'loginMethod'
}
],
order: [ [1, 'asc'] ],
drawCallback: function (settings) {
const api = new DataTable.Api(settings);
api.rows().every(function (row) {
const node = this.node();
const data = this.data();
node.addEventListener('click', (e) => {
displayEdit(data);
});
});
}
});
const button = <HTMLButtonElement>document.querySelector('#edit-modal .js--save');
if (!button) {
return;
}
button.addEventListener('click', async (e) => {
const permissionEditor = <HTMLSelectElement>document.querySelector('#edit-modal .permission-editor');
if (!permissionEditor) {
return;
}
const formData = new FormData();
formData.set('permission', permissionEditor.value);
const response = await fetch(
`/api/web/users/${button.dataset.userid}`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
Modal.getOrCreateInstance('#edit-modal').hide();
dt.draw();
})
})

View file

@ -1,2 +1,2 @@
import '../../css/common/index.scss';
import '../../css/common/index.scss';
import '../common/index';

View file

@ -1,148 +1,148 @@
import {getCurrentlySelectedList} from "./userlists";
import {getTableAPI} from "./table";
async function checkImportFile() {
const fileInput = document.querySelector<HTMLInputElement>('#import');
if (!fileInput) {
return;
}
if (!fileInput.files || !fileInput.files[0]) {
return;
}
const formData = new FormData();
formData.set('file', fileInput.files[0]);
const response = await fetch(
`/api/web/keys/import/prepare`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
const jsonData = await response.json();
const container = document.getElementById('import-info-container');
if (!container) {
return
}
container.classList.remove('d-none');
createTableContents(jsonData);
}
function createTableContents(data: {index: string, displayName: string, guessedAttribute: string}[]) {
const table = document.querySelector<HTMLTableElement>('#import-attribute-table');
if (!table) {
return;
}
const tbody = table.querySelector<HTMLTableSectionElement>('tbody');
if (!tbody) {
return;
}
for (let child of tbody.children) {
child.remove();
}
const keyAttributeMeta = document.querySelector<HTMLMetaElement>('meta[name="key-attributes"]')?.content;
const attributes = JSON.parse(keyAttributeMeta ?? '');
const select = document.createElement('select');
select.classList.add('form-select', 'w-100');
for (let attributeName in attributes) {
const attributeValue = attributes[attributeName];
const option = document.createElement('option');
option.value = attributeValue;
option.textContent = attributeName;
select.add(option);
}
data.forEach((datum) => {
const row = tbody.insertRow();
const indexCell = row.insertCell();
indexCell.classList.add('index-cell');
indexCell.innerText = datum.index;
const displayNameCEll = row.insertCell();
displayNameCEll.classList.add('name-cell');
displayNameCEll.innerText = datum.displayName;
const attributeCell = row.insertCell();
attributeCell.classList.add('attribute-cell');
const rowSelect = <HTMLSelectElement>select.cloneNode(true);
rowSelect.value = datum.guessedAttribute;
attributeCell.appendChild(rowSelect);
})
}
async function doImport() {
const fileInput = document.querySelector<HTMLInputElement>('#import');
if (!fileInput) {
return;
}
if (!fileInput.files || !fileInput.files[0]) {
return;
}
const formData = new FormData();
formData.set('file', fileInput.files[0]);
const tbody = document.querySelector<HTMLTableSectionElement>('#import-attribute-table tbody');
if (!tbody) {
return;
}
for (let row of tbody.querySelectorAll('tr')) {
const columnIndex = row.querySelector('.index-cell')?.textContent;
const attribute = row.querySelector<HTMLSelectElement>('.attribute-cell select')?.value ?? 'none';
formData.set(`columns[${columnIndex}]`, attribute);
}
const listId = getCurrentlySelectedList() ?? 0;
formData.set('listid', listId.toString());
const response = await fetch(
`/api/web/keys/import/perform`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
const container = document.getElementById('import-info-container');
if (!container) {
return
}
container.classList.add('d-none');
fileInput.value = '';
getTableAPI().ajax.reload();
}
export function init() {
const importButton = document.querySelector('.js--send-import');
importButton?.addEventListener('click', checkImportFile);
const doImportButton = document.querySelector('.js--do-import');
doImportButton?.addEventListener('click', doImport);
import {getCurrentlySelectedList} from "./userlists";
import {getTableAPI} from "./table";
async function checkImportFile() {
const fileInput = document.querySelector<HTMLInputElement>('#import');
if (!fileInput) {
return;
}
if (!fileInput.files || !fileInput.files[0]) {
return;
}
const formData = new FormData();
formData.set('file', fileInput.files[0]);
const response = await fetch(
`/api/web/keys/import/prepare`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
const jsonData = await response.json();
const container = document.getElementById('import-info-container');
if (!container) {
return
}
container.classList.remove('d-none');
createTableContents(jsonData);
}
function createTableContents(data: {index: string, displayName: string, guessedAttribute: string}[]) {
const table = document.querySelector<HTMLTableElement>('#import-attribute-table');
if (!table) {
return;
}
const tbody = table.querySelector<HTMLTableSectionElement>('tbody');
if (!tbody) {
return;
}
for (let child of tbody.children) {
child.remove();
}
const keyAttributeMeta = document.querySelector<HTMLMetaElement>('meta[name="key-attributes"]')?.content;
const attributes = JSON.parse(keyAttributeMeta ?? '');
const select = document.createElement('select');
select.classList.add('form-select', 'w-100');
for (let attributeName in attributes) {
const attributeValue = attributes[attributeName];
const option = document.createElement('option');
option.value = attributeValue;
option.textContent = attributeName;
select.add(option);
}
data.forEach((datum) => {
const row = tbody.insertRow();
const indexCell = row.insertCell();
indexCell.classList.add('index-cell');
indexCell.innerText = datum.index;
const displayNameCEll = row.insertCell();
displayNameCEll.classList.add('name-cell');
displayNameCEll.innerText = datum.displayName;
const attributeCell = row.insertCell();
attributeCell.classList.add('attribute-cell');
const rowSelect = <HTMLSelectElement>select.cloneNode(true);
rowSelect.value = datum.guessedAttribute;
attributeCell.appendChild(rowSelect);
})
}
async function doImport() {
const fileInput = document.querySelector<HTMLInputElement>('#import');
if (!fileInput) {
return;
}
if (!fileInput.files || !fileInput.files[0]) {
return;
}
const formData = new FormData();
formData.set('file', fileInput.files[0]);
const tbody = document.querySelector<HTMLTableSectionElement>('#import-attribute-table tbody');
if (!tbody) {
return;
}
for (let row of tbody.querySelectorAll('tr')) {
const columnIndex = row.querySelector('.index-cell')?.textContent;
const attribute = row.querySelector<HTMLSelectElement>('.attribute-cell select')?.value ?? 'none';
formData.set(`columns[${columnIndex}]`, attribute);
}
const listId = getCurrentlySelectedList() ?? 0;
formData.set('listid', listId.toString());
const response = await fetch(
`/api/web/keys/import/perform`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
const container = document.getElementById('import-info-container');
if (!container) {
return
}
container.classList.add('d-none');
fileInput.value = '';
getTableAPI().ajax.reload();
}
export function init() {
const importButton = document.querySelector('.js--send-import');
importButton?.addEventListener('click', checkImportFile);
const doImportButton = document.querySelector('.js--do-import');
doImportButton?.addEventListener('click', doImport);
}

View file

@ -1,24 +1,26 @@
import '../../../css/common/index.scss';
import '../../common/index';
import { Tab } from 'bootstrap';
import {init as initImport} from "./import";
import {init as initTable} from "./table";
import {init as initUserLists} from "./userlists";
document.addEventListener('DOMContentLoaded', () => {
const triggerTabList = document.querySelectorAll('#key-tab button')
triggerTabList.forEach(triggerEl => {
const tabTrigger = new Tab(triggerEl)
triggerEl.addEventListener('click', event => {
event.preventDefault()
tabTrigger.show()
})
})
initImport();
initTable();
initUserLists();
import '../../../css/common/index.scss';
import '../../common/index';
import { Tab } from 'bootstrap';
import {init as initImport} from "./import";
import {init as initTable} from "./table";
import {init as initUserLists} from "./userlists";
import {initShare} from "./share";
document.addEventListener('DOMContentLoaded', () => {
const triggerTabList = document.querySelectorAll('#key-tab button')
triggerTabList.forEach(triggerEl => {
const tabTrigger = new Tab(triggerEl)
triggerEl.addEventListener('click', event => {
event.preventDefault()
tabTrigger.show()
})
})
initImport();
initTable();
initUserLists();
initShare();
})

View file

@ -0,0 +1,24 @@
import DataTable from "datatables.net-bs5";
export function initShare() {
const table = document.querySelector<HTMLTableElement>('#shared-users-table');
if (!table) {
return;
}
new DataTable(table, {
columns: [
{
name: '',
data: 'icon',
searchable: false,
orderable: false,
},
{
name: 'Name',
data: 'name',
}
],
order: [ [1, "desc"] ]
});
}

View file

@ -1,108 +1,108 @@
import DataTable, {Api} from "datatables.net-bs5";
import 'datatables.net-bs5/css/dataTables.bootstrap5.css';
import {Key} from "../types/entities";
import {getIconForKeyState, getKeyStateExplanation} from "../types/keyState";
import {Dropdown, Tooltip} from "bootstrap";
import {getCurrentlySelectedList} from "./userlists";
const TABLE_AJAX_URL = '/api/dt/keys/provider';
let tableAPI: Api;
export function getTableAPI(): Api {
return tableAPI;
}
function getKeyDisplay(parent: HTMLElement, keys: Key[]) {
const header = document.createElement("h1");
header.innerText = 'Keys';
header.classList.add('h6');
const table = document.createElement("table");
table.classList.add('table', 'table-striped', 'w-100');
const body = table.createTBody();
for (const {fromWhere, state, key, store, store_link} of keys) {
const row = body.insertRow();
row.classList.add('cursor-pointer');
const stateCell = row.insertCell();
stateCell.classList.add('w-auto', 'align-content-center');
stateCell.innerHTML = `<a data-bs-toggle="tooltip" data-bs-title="${getKeyStateExplanation(state)}"><i class="fa-solid ${ getIconForKeyState(state) }"></i></a>`
const anchor = stateCell.querySelector('a');
if (!anchor) {
return;
}
new Tooltip(anchor);
row.insertCell().textContent = key;
row.insertCell().textContent = store === 'external' ? store_link : store;
row.insertCell().textContent = fromWhere;
row.addEventListener('click', () => {
console.log('Key options');
})
}
parent.appendChild(header);
parent.appendChild(table);
}
export function init() {
const keyTable = document.querySelector<HTMLTableElement>('.key-table');
if (!keyTable) {
return;
}
const table = tableAPI = new DataTable(keyTable, {
ajax: {
url: TABLE_AJAX_URL,
data: function (d) {
// @ts-ignore
d.listid = getCurrentlySelectedList();
}
},
processing: true,
columns: [
{
data: 'gamePicture',
searchable: false
},
{
data: 'name',
},
{
data: 'keysAmount',
searchable: false,
},
{
data: 'igdbState',
searchable: false,
}
],
ordering: false,
order: [ [1, 'asc'] ],
createdRow(row: Node, data: any) {
const tableRow = <HTMLTableRowElement>row;
tableRow.classList.add('cursor-pointer');
tableRow.addEventListener('click', () => {
const rowAPI = table.row(row);
if (rowAPI.child.isShown()) {
rowAPI.child.hide();
return;
}
const childRow = document.createElement('tr');
const cell = childRow.insertCell();
cell.colSpan = row.childNodes.length;
getKeyDisplay(cell, data.keys);
rowAPI.child(childRow).show();
})
},
});
import DataTable, {Api} from "datatables.net-bs5";
import 'datatables.net-bs5/css/dataTables.bootstrap5.css';
import {Key} from "../types/entities";
import {getIconForKeyState, getKeyStateExplanation} from "../types/keyState";
import {Dropdown, Tooltip} from "bootstrap";
import {getCurrentlySelectedList} from "./userlists";
const TABLE_AJAX_URL = '/api/dt/keys/provider';
let tableAPI: Api;
export function getTableAPI(): Api {
return tableAPI;
}
function getKeyDisplay(parent: HTMLElement, keys: Key[]) {
const header = document.createElement("h1");
header.innerText = 'Keys';
header.classList.add('h6');
const table = document.createElement("table");
table.classList.add('table', 'table-striped', 'w-100');
const body = table.createTBody();
for (const {fromWhere, state, key, store, store_link} of keys) {
const row = body.insertRow();
row.classList.add('cursor-pointer');
const stateCell = row.insertCell();
stateCell.classList.add('w-auto', 'align-content-center');
stateCell.innerHTML = `<a data-bs-toggle="tooltip" data-bs-title="${getKeyStateExplanation(state)}"><i class="fa-solid ${ getIconForKeyState(state) }"></i></a>`
const anchor = stateCell.querySelector('a');
if (!anchor) {
return;
}
new Tooltip(anchor);
row.insertCell().textContent = key;
row.insertCell().textContent = store === 'external' ? store_link : store;
row.insertCell().textContent = fromWhere;
row.addEventListener('click', () => {
console.log('Key options');
})
}
parent.appendChild(header);
parent.appendChild(table);
}
export function init() {
const keyTable = document.querySelector<HTMLTableElement>('.key-table');
if (!keyTable) {
return;
}
const table = tableAPI = new DataTable(keyTable, {
ajax: {
url: TABLE_AJAX_URL,
data: function (d) {
// @ts-ignore
d.listid = getCurrentlySelectedList();
}
},
processing: true,
columns: [
{
data: 'gamePicture',
searchable: false,
orderable: false
},
{
data: 'name',
},
{
data: 'keysAmount',
searchable: false,
},
{
data: 'igdbState',
searchable: false,
}
],
order: [ [1, 'asc'] ],
createdRow(row: Node, data: any) {
const tableRow = <HTMLTableRowElement>row;
tableRow.classList.add('cursor-pointer');
tableRow.addEventListener('click', () => {
const rowAPI = table.row(row);
if (rowAPI.child.isShown()) {
rowAPI.child.hide();
return;
}
const childRow = document.createElement('tr');
const cell = childRow.insertCell();
cell.colSpan = row.childNodes.length;
getKeyDisplay(cell, data.keys);
rowAPI.child(childRow).show();
})
},
});
}

View file

@ -1,71 +1,71 @@
import {Modal} from "bootstrap";
import {getTableAPI} from "./table";
export function getCurrentlySelectedList(): number|null {
const listSelect = document.querySelector<HTMLSelectElement>('#list-select');
if (!listSelect) {
return null;
}
return parseInt(listSelect.value);
}
export function init() {
const modal = document.querySelector('#create-list-modal');
if (!modal) {
return;
}
const modalObj = new Modal(modal);
modal.addEventListener('show.bs.modal', (e) => {
const input = modal.querySelector<HTMLInputElement>('#createListName');
if (!input) {
return;
}
input.value = '';
})
modal.querySelector('.js--create-list')?.addEventListener('click', async (e) => {
const input = modal.querySelector<HTMLInputElement>('#createListName');
if (!input) {
return;
}
const newName = input.value;
const formData = new FormData();
formData.append('name', newName);
const response = await fetch(
`/api/web/keys/list/create`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
window.location.reload();
});
const listSelect = document.querySelector<HTMLSelectElement>('#list-select');
if (listSelect) {
listSelect.addEventListener('change', (e) => {
if (listSelect.value === '_create') {
modalObj.show()
return;
}
getTableAPI().ajax.reload();
})
}
const newButton = document.querySelector('.js--create-list-button');
if (newButton) {
newButton.addEventListener('click', () => modalObj.show())
}
import {Modal} from "bootstrap";
import {getTableAPI} from "./table";
export function getCurrentlySelectedList(): number|null {
const listSelect = document.querySelector<HTMLSelectElement>('#list-select');
if (!listSelect) {
return null;
}
return parseInt(listSelect.value);
}
export function init() {
const modal = document.querySelector('#create-list-modal');
if (!modal) {
return;
}
const modalObj = new Modal(modal);
modal.addEventListener('show.bs.modal', (e) => {
const input = modal.querySelector<HTMLInputElement>('#createListName');
if (!input) {
return;
}
input.value = '';
})
modal.querySelector('.js--create-list')?.addEventListener('click', async (e) => {
const input = modal.querySelector<HTMLInputElement>('#createListName');
if (!input) {
return;
}
const newName = input.value;
const formData = new FormData();
formData.append('name', newName);
const response = await fetch(
`/api/web/keys/list/create`,
{
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(response.statusText);
}
window.location.reload();
});
const listSelect = document.querySelector<HTMLSelectElement>('#list-select');
if (listSelect) {
listSelect.addEventListener('change', (e) => {
if (listSelect.value === '_create') {
modalObj.show()
return;
}
getTableAPI().ajax.reload();
})
}
const newButton = document.querySelector('.js--create-list-button');
if (newButton) {
newButton.addEventListener('click', () => modalObj.show())
}
}

View file

@ -1,25 +1,25 @@
import {KeyState} from "./keyState";
enum Store {
STEAM = 'steam',
GOG = 'gog',
EPICGAMES = 'epicgames',
ORIGIN = 'origin',
UPLAY = 'uplay',
BATTLENET = 'battlenet',
EXTERNAL = 'external'
}
export type Game = {
name: string
}
export type Key = {
game?: Game,
key: string,
store: Store,
state: KeyState,
store_link: string|null,
fromWhere: string|null,
}
import {KeyState} from "./keyState";
enum Store {
STEAM = 'steam',
GOG = 'gog',
EPICGAMES = 'epicgames',
ORIGIN = 'origin',
UPLAY = 'uplay',
BATTLENET = 'battlenet',
EXTERNAL = 'external'
}
export type Game = {
name: string
}
export type Key = {
game?: Game,
key: string,
store: Store,
state: KeyState,
store_link: string|null,
fromWhere: string|null,
}

View file

@ -1,35 +1,35 @@
export enum KeyState {
AVAILABLE = 1,
UNKNOWN = 0,
RESERVED_FOR_GIFT = -1,
CLAIMED = -10
}
export function getIconForKeyState(keyState: KeyState): string {
switch (keyState) {
case KeyState.AVAILABLE:
return "fa-check text-success";
default:
case KeyState.UNKNOWN:
return 'fa-question text-info';
case KeyState.RESERVED_FOR_GIFT:
return 'fa-gift text-warning';
case KeyState.CLAIMED:
return 'fa-x text-danger';
}
}
export function getKeyStateExplanation(keyState: KeyState): string {
switch (keyState) {
case KeyState.AVAILABLE:
return "This key is available";
default:
case KeyState.UNKNOWN:
return 'The state of this key is unknown';
case KeyState.RESERVED_FOR_GIFT:
return 'This key is reserved for a gift';
case KeyState.CLAIMED:
return 'This key was claimed';
}
export enum KeyState {
AVAILABLE = 1,
UNKNOWN = 0,
RESERVED_FOR_GIFT = -1,
CLAIMED = -10
}
export function getIconForKeyState(keyState: KeyState): string {
switch (keyState) {
case KeyState.AVAILABLE:
return "fa-check text-success";
default:
case KeyState.UNKNOWN:
return 'fa-question text-info';
case KeyState.RESERVED_FOR_GIFT:
return 'fa-gift text-warning';
case KeyState.CLAIMED:
return 'fa-x text-danger';
}
}
export function getKeyStateExplanation(keyState: KeyState): string {
switch (keyState) {
case KeyState.AVAILABLE:
return "This key is available";
default:
case KeyState.UNKNOWN:
return 'The state of this key is unknown';
case KeyState.RESERVED_FOR_GIFT:
return 'This key is reserved for a gift';
case KeyState.CLAIMED:
return 'This key was claimed';
}
}

View file

@ -1,79 +1,79 @@
<?php
declare(strict_types=1);
namespace GamesShop\Api;
use Curl\Curl;
use Exception;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Errors\ExtendedException;
final class DiscordAPI
{
const string OAUTH_TOKEN_URL = "https://discord.com/api/oauth2/token";
const string USER_ME_URL = 'https://discord.com/api/users/@me';
public function __construct(
private readonly EnvironmentHandler $env
)
{
}
/**
* @return array{id: string, global_name: string, avatar: string, discriminator: int}
* @throws Exception
*/
public function getUserFromCode(string $code, string $redirectUri): array {
$discordEnv = $this->env->getDiscordEnvironment();
$curl = new Curl();
$curl->setHeader('Content-Type', 'application/x-www-form-urlencoded');
$curl->setBasicAuthentication($discordEnv->clientId, $discordEnv->clientSecret);
$data =[
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirectUri
];
$curl->post(self::OAUTH_TOKEN_URL, $data);
if ($curl->error) {
$curl->diagnose();
throw new ExtendedException($curl->errorMessage, [ 'response' => $curl->response, 'data' => $data ]);
}
$accessToken = $curl->response->access_token;
$tokenType = $curl->response->token_type;
$curl = new Curl();
$curl->setHeader("authorization", "$tokenType $accessToken");
$curl->get(self::USER_ME_URL);
if ($curl->error) {
$curl->diagnose();
throw new ExtendedException($curl->errorMessage, [ 'response' => $curl->response, ]);
}
return [
'id' => $curl->response->id,
'global_name' => $curl->response->global_name,
'avatar' => $curl->response->avatar,
'discriminator' => (int) $curl->response->discriminator
];
}
public function getAvatarURL(string $userId, string|int $avatarHash) {
if (is_int($avatarHash)) {
return "https://cdn.discordapp.com/embed/avatars/{$avatarHash}.png";
}
$extension = 'png';
if (str_starts_with($avatarHash, 'a_')) {
$extension = 'gif';
}
return "https://cdn.discordapp.com/avatars/{$userId}/{$avatarHash}.{$extension}";
}
<?php
declare(strict_types=1);
namespace GamesShop\Api;
use Curl\Curl;
use Exception;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Errors\ExtendedException;
final class DiscordAPI
{
const string OAUTH_TOKEN_URL = "https://discord.com/api/oauth2/token";
const string USER_ME_URL = 'https://discord.com/api/users/@me';
public function __construct(
private readonly EnvironmentHandler $env
)
{
}
/**
* @return array{id: string, global_name: string, avatar: string, discriminator: int}
* @throws Exception
*/
public function getUserFromCode(string $code, string $redirectUri): array {
$discordEnv = $this->env->getDiscordEnvironment();
$curl = new Curl();
$curl->setHeader('Content-Type', 'application/x-www-form-urlencoded');
$curl->setBasicAuthentication($discordEnv->clientId, $discordEnv->clientSecret);
$data =[
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirectUri
];
$curl->post(self::OAUTH_TOKEN_URL, $data);
if ($curl->error) {
$curl->diagnose();
throw new ExtendedException($curl->errorMessage, [ 'response' => $curl->response, 'data' => $data ]);
}
$accessToken = $curl->response->access_token;
$tokenType = $curl->response->token_type;
$curl = new Curl();
$curl->setHeader("authorization", "$tokenType $accessToken");
$curl->get(self::USER_ME_URL);
if ($curl->error) {
$curl->diagnose();
throw new ExtendedException($curl->errorMessage, [ 'response' => $curl->response, ]);
}
return [
'id' => $curl->response->id,
'global_name' => $curl->response->global_name,
'avatar' => $curl->response->avatar,
'discriminator' => (int) $curl->response->discriminator
];
}
public function getAvatarURL(string $userId, string|int $avatarHash) {
if (is_int($avatarHash)) {
return "https://cdn.discordapp.com/embed/avatars/{$avatarHash}.png";
}
$extension = 'png';
if (str_starts_with($avatarHash, 'a_')) {
$extension = 'gif';
}
return "https://cdn.discordapp.com/avatars/{$userId}/{$avatarHash}.{$extension}";
}
}

View file

@ -1,39 +1,39 @@
<?php
declare(strict_types=1);
namespace GamesShop;
use League\Container\Container;
use League\Container\ReflectionContainer;
final class ContainerHandler
{
private static Container|null $instance = null;
public static function getInstance(): Container
{
if (self::$instance === null) {
self::createInstance();
}
return self::$instance;
}
/**
* @template RequestedType
*
* @param class-string<RequestedType>|string $id
*
* @return RequestedType|mixed
*/
public static function get(string $id) {
return self::getInstance()->get($id);
}
private static function createInstance()
{
self::$instance = new Container();
$reflectionContainer = new ReflectionContainer(true);
self::$instance->delegate($reflectionContainer);
}
<?php
declare(strict_types=1);
namespace GamesShop;
use League\Container\Container;
use League\Container\ReflectionContainer;
final class ContainerHandler
{
private static Container|null $instance = null;
public static function getInstance(): Container
{
if (self::$instance === null) {
self::createInstance();
}
return self::$instance;
}
/**
* @template RequestedType
*
* @param class-string<RequestedType>|string $id
*
* @return RequestedType|mixed
*/
public static function get(string $id) {
return self::getInstance()->get($id);
}
private static function createInstance()
{
self::$instance = new Container();
$reflectionContainer = new ReflectionContainer(true);
self::$instance->delegate($reflectionContainer);
}
}

View file

@ -1,35 +1,35 @@
<?php
declare(strict_types=1);
namespace GamesShop;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMSetup;
use GamesShop\Environment\EnvironmentHandler;
final class DoctrineManager
{
public function setup() {
$container = ContainerHandler::getInstance();
$environmentHandler = $container->get(EnvironmentHandler::class);
$config = ORMSetup::createAttributeMetadataConfiguration(
paths: [ Paths::PHP_SOURCE_PATH . '/Entities' ],
isDevMode: !$environmentHandler->isProduction()
);
$dbEnvironment = $environmentHandler->getDatabaseEnvironment();
$connection = DriverManager::getConnection($dbEnvironment->getDoctrineConfig());
$entityManager = new EntityManager($connection, $config);
$container->addShared(EntityManager::class, $entityManager);
$container->addShared(EntityManagerInterface::class, $entityManager);
$container->addShared(Connection::class, $connection);
}
<?php
declare(strict_types=1);
namespace GamesShop;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMSetup;
use GamesShop\Environment\EnvironmentHandler;
final class DoctrineManager
{
public function setup() {
$container = ContainerHandler::getInstance();
$environmentHandler = $container->get(EnvironmentHandler::class);
$config = ORMSetup::createAttributeMetadataConfiguration(
paths: [ Paths::PHP_SOURCE_PATH . '/Entities' ],
isDevMode: !$environmentHandler->isProduction()
);
$dbEnvironment = $environmentHandler->getDatabaseEnvironment();
$connection = DriverManager::getConnection($dbEnvironment->getDoctrineConfig());
$entityManager = new EntityManager($connection, $config);
$container->addShared(EntityManager::class, $entityManager);
$container->addShared(EntityManagerInterface::class, $entityManager);
$container->addShared(Connection::class, $connection);
}
}

View file

@ -1,85 +1,85 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Account;
use Doctrine\ORM\Mapping as ORM;
use GamesShop\Login\LoginMethod;
use GamesShop\Login\UserPermission;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV4;
#[ORM\Entity]
#[ORM\Table(name: "users")]
final class User
{
#[ORM\Id()]
#[ORM\Column]
#[ORM\GeneratedValue]
private int|null $id = null;
#[ORM\Column(type: 'integer', enumType: LoginMethod::class)]
private LoginMethod $loginMethod;
#[ORM\Column]
private string|null $foreignLoginId = null;
#[ORM\Column]
private string $name;
#[ORM\Column]
private string $profilePictureUrl;
#[ORM\Column]
private UserPermission $permission;
public function __construct(LoginMethod $loginMethod, ?string $foreignLoginId, string $name, string $profilePictureUrl, UserPermission $permission)
{
$this->loginMethod = $loginMethod;
$this->foreignLoginId = $foreignLoginId;
$this->name = $name;
$this->profilePictureUrl = $profilePictureUrl;
$this->permission = $permission;
}
public function getId(): ?int
{
return $this->id;
}
public function getLoginMethod(): LoginMethod
{
return $this->loginMethod;
}
public function getForeignLoginId(): ?string
{
return $this->foreignLoginId;
}
public function getName(): string
{
return $this->name;
}
public function getProfilePictureUrl(): string
{
return $this->profilePictureUrl;
}
public function getPermission(): UserPermission
{
return $this->permission;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function setProfilePictureUrl(string $profilePictureUrl): void
{
$this->profilePictureUrl = $profilePictureUrl;
}
public function setPermission(UserPermission $permission): void
{
$this->permission = $permission;
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Account;
use Doctrine\ORM\Mapping as ORM;
use GamesShop\Login\LoginMethod;
use GamesShop\Login\UserPermission;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV4;
#[ORM\Entity]
#[ORM\Table(name: "users")]
final class User
{
#[ORM\Id()]
#[ORM\Column]
#[ORM\GeneratedValue]
private int|null $id = null;
#[ORM\Column(type: 'integer', enumType: LoginMethod::class)]
private LoginMethod $loginMethod;
#[ORM\Column]
private string|null $foreignLoginId = null;
#[ORM\Column]
private string $name;
#[ORM\Column]
private string $profilePictureUrl;
#[ORM\Column]
private UserPermission $permission;
public function __construct(LoginMethod $loginMethod, ?string $foreignLoginId, string $name, string $profilePictureUrl, UserPermission $permission)
{
$this->loginMethod = $loginMethod;
$this->foreignLoginId = $foreignLoginId;
$this->name = $name;
$this->profilePictureUrl = $profilePictureUrl;
$this->permission = $permission;
}
public function getId(): ?int
{
return $this->id;
}
public function getLoginMethod(): LoginMethod
{
return $this->loginMethod;
}
public function getForeignLoginId(): ?string
{
return $this->foreignLoginId;
}
public function getName(): string
{
return $this->name;
}
public function getProfilePictureUrl(): string
{
return $this->profilePictureUrl;
}
public function getPermission(): UserPermission
{
return $this->permission;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function setProfilePictureUrl(string $profilePictureUrl): void
{
$this->profilePictureUrl = $profilePictureUrl;
}
public function setPermission(UserPermission $permission): void
{
$this->permission = $permission;
}
}

View file

@ -1,36 +1,36 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'games')]
class Game
{
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue]
private int|null $id;
#[ORM\Column]
private string $name;
public function getName(): string
{
return $this->name;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @param string $name
*/
public function __construct(string $name)
{
$this->name = $name;
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'games')]
class Game
{
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue]
private int|null $id;
#[ORM\Column]
private string $name;
public function getName(): string
{
return $this->name;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @param string $name
*/
public function __construct(string $name)
{
$this->name = $name;
}
}

View file

@ -1,96 +1,96 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
use Doctrine\ORM\Mapping as ORM;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\GamesList;
use JsonSerializable;
#[ORM\Entity]
#[ORM\Table(name: 'keys')]
final class Key implements JsonSerializable
{
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue]
private int|null $id;
#[ORM\ManyToOne]
private Game $game;
#[ORM\ManyToOne]
private GamesList $list;
#[ORM\Column]
private string $key;
#[ORM\Column(type: 'string', enumType: Store::class)]
private Store $store;
#[ORM\Column(nullable: true)]
private string|null $storeLink;
#[ORM\Column]
private string|null $fromWhere;
#[ORM\Column(type: 'integer', enumType: KeyState::class)]
private KeyState $state;
public function __construct(Game $game, GamesList $list, string $key, Store $store, ?string $storeLink, ?string $fromWhere)
{
$this->game = $game;
$this->list = $list;
$this->key = $key;
$this->store = $store;
$this->storeLink = $storeLink;
$this->fromWhere = $fromWhere;
$this->state = KeyState::AVAILABLE;
}
public function getId(): ?int
{
return $this->id;
}
public function getGame(): Game
{
return $this->game;
}
public function getContributedUser(): User
{
return $this->contributedUser;
}
public function getKey(): string
{
return $this->key;
}
public function getStore(): Store
{
return $this->store;
}
public function getStoreLink(): ?string
{
return $this->storeLink;
}
public function getFromWhere(): ?string
{
return $this->fromWhere;
}
public function getState(): KeyState
{
return $this->state;
}
public function jsonSerialize(): mixed
{
return [
'id' => $this->id,
'key' => $this->key,
'store' => $this->store->value,
'store_link' => $this->storeLink,
'from_where' => $this->fromWhere,
'state' => $this->state->value,
];
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
use Doctrine\ORM\Mapping as ORM;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\GamesList;
use JsonSerializable;
#[ORM\Entity]
#[ORM\Table(name: 'keys')]
final class Key implements JsonSerializable
{
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue]
private int|null $id;
#[ORM\ManyToOne]
private Game $game;
#[ORM\ManyToOne]
private GamesList $list;
#[ORM\Column]
private string $key;
#[ORM\Column(type: 'string', enumType: Store::class)]
private Store $store;
#[ORM\Column(nullable: true)]
private string|null $storeLink;
#[ORM\Column]
private string|null $fromWhere;
#[ORM\Column(type: 'integer', enumType: KeyState::class)]
private KeyState $state;
public function __construct(Game $game, GamesList $list, string $key, Store $store, ?string $storeLink, ?string $fromWhere)
{
$this->game = $game;
$this->list = $list;
$this->key = $key;
$this->store = $store;
$this->storeLink = $storeLink;
$this->fromWhere = $fromWhere;
$this->state = KeyState::AVAILABLE;
}
public function getId(): ?int
{
return $this->id;
}
public function getGame(): Game
{
return $this->game;
}
public function getContributedUser(): User
{
return $this->contributedUser;
}
public function getKey(): string
{
return $this->key;
}
public function getStore(): Store
{
return $this->store;
}
public function getStoreLink(): ?string
{
return $this->storeLink;
}
public function getFromWhere(): ?string
{
return $this->fromWhere;
}
public function getState(): KeyState
{
return $this->state;
}
public function jsonSerialize(): mixed
{
return [
'id' => $this->id,
'key' => $this->key,
'store' => $this->store->value,
'store_link' => $this->storeLink,
'from_where' => $this->fromWhere,
'state' => $this->state->value,
];
}
}

View file

@ -1,22 +1,22 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
enum KeyAttribute: string
{
case NONE = 'none';
case GAME_NAME = "game_name";
case KEY = "key";
case STORE = 'store';
case FROM = 'from';
public static function casesAsAssociative(): array {
$result = [];
foreach (self::cases() as $case) {
$result[$case->name] = $case->value;
}
return $result;
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
enum KeyAttribute: string
{
case NONE = 'none';
case GAME_NAME = "game_name";
case KEY = "key";
case STORE = 'store';
case FROM = 'from';
public static function casesAsAssociative(): array {
$result = [];
foreach (self::cases() as $case) {
$result[$case->name] = $case->value;
}
return $result;
}
}

View file

@ -1,12 +1,12 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
enum KeyState: int
{
case AVAILABLE = 1;
case UNKNOWN = 0;
case RESERVED_FOR_GIFT = -1;
case CLAIMED = -10;
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
enum KeyState: int
{
case AVAILABLE = 1;
case UNKNOWN = 0;
case RESERVED_FOR_GIFT = -1;
case CLAIMED = -10;
}

View file

@ -1,15 +1,15 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
enum Store: string
{
case STEAM = 'steam';
case GOG = 'gog';
case EPICGAMES = 'epicgames';
case ORIGIN = 'origin';
case UPLAY = 'uplay';
case BATTLENET = 'battlenet';
case EXTERNAL = 'external';
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
enum Store: string
{
case STEAM = 'steam';
case GOG = 'gog';
case EPICGAMES = 'epicgames';
case ORIGIN = 'origin';
case UPLAY = 'uplay';
case BATTLENET = 'battlenet';
case EXTERNAL = 'external';
}

View file

@ -1,62 +1,62 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use GamesShop\Entities\Account\User;
#[ORM\Entity]
#[ORM\Table(name: 'games_lists')]
final class GamesList
{
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue]
private int|null $id;
#[ORM\ManyToOne]
private User $owner;
#[ORM\Column(nullable: true)]
private string|null $name;
#[ORM\JoinTable(name: 'games_list_claimer')]
#[ORM\JoinColumn(name: 'id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: User::class)]
private Collection $claimer;
/**
* @param User $owner
* @param string|null $name
*/
public function __construct(User $owner, ?string $name)
{
$this->owner = $owner;
$this->name = $name;
}
public function getId(): ?int
{
return $this->id;
}
public function getOwner(): User
{
return $this->owner;
}
public function getName(): ?string
{
return $this->name;
}
public function getClaimer(): array
{
return $this->claimer;
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use GamesShop\Entities\Account\User;
#[ORM\Entity]
#[ORM\Table(name: 'games_lists')]
final class GamesList
{
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue]
private int|null $id;
#[ORM\ManyToOne]
private User $owner;
#[ORM\Column(nullable: true)]
private string|null $name;
#[ORM\JoinTable(name: 'games_list_claimer')]
#[ORM\JoinColumn(name: 'id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: User::class)]
private Collection $claimer;
/**
* @param User $owner
* @param string|null $name
*/
public function __construct(User $owner, ?string $name)
{
$this->owner = $owner;
$this->name = $name;
}
public function getId(): ?int
{
return $this->id;
}
public function getOwner(): User
{
return $this->owner;
}
public function getName(): ?string
{
return $this->name;
}
public function getClaimer(): array
{
return $this->claimer;
}
}

View file

@ -1,34 +1,34 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'attributes')]
final class SystemAttribute
{
#[ORM\Id]
#[ORM\Column]
private string $name;
#[ORM\Column]
private string $value;
public function __construct(string $name, string $value)
{
$this->name = $name;
$this->value = $value;
}
public function getName(): string
{
return $this->name;
}
public function getValue(): string
{
return $this->value;
}
<?php
declare(strict_types=1);
namespace GamesShop\Entities;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'attributes')]
final class SystemAttribute
{
#[ORM\Id]
#[ORM\Column]
private string $name;
#[ORM\Column]
private string $value;
public function __construct(string $name, string $value)
{
$this->name = $name;
$this->value = $value;
}
public function getName(): string
{
return $this->name;
}
public function getValue(): string
{
return $this->value;
}
}

View file

@ -1,21 +1,21 @@
<?php
declare(strict_types=1);
namespace GamesShop\Environment;
final readonly class DatabaseEnvironment
{
public function __construct(
public string $driver,
public string $path
)
{
}
public function getDoctrineConfig() {
return [
'driver' => $this->driver,
'path' => $this->path
];
}
<?php
declare(strict_types=1);
namespace GamesShop\Environment;
final readonly class DatabaseEnvironment
{
public function __construct(
public string $driver,
public string $path
)
{
}
public function getDoctrineConfig() {
return [
'driver' => $this->driver,
'path' => $this->path
];
}
}

View file

@ -1,13 +1,13 @@
<?php
declare(strict_types=1);
namespace GamesShop\Environment;
final readonly class DiscordEnvironment
{
public function __construct(
public string $clientId,
public string $clientSecret,
public string $loginUrl,
) {}
<?php
declare(strict_types=1);
namespace GamesShop\Environment;
final readonly class DiscordEnvironment
{
public function __construct(
public string $clientId,
public string $clientSecret,
public string $loginUrl,
) {}
}

View file

@ -1,40 +1,40 @@
<?php
declare(strict_types=1);
namespace GamesShop\Environment;
use DotenvVault\DotenvVault;
use GamesShop\Paths;
final class EnvironmentHandler
{
private const string ENVIRONMENT_PATH = Paths::ROOT_PATH . '/config';
public function load() {
$dotEnv = DotenvVault::createImmutable(
self::ENVIRONMENT_PATH
);
$dotEnv->safeLoad();
}
public function getDiscordEnvironment(): DiscordEnvironment {
return new DiscordEnvironment(
$_SERVER['DISCORD_CLIENT_ID'],
$_SERVER['DISCORD_CLIENT_SECRET'],
$_SERVER['DISCORD_CLIENT_LOGIN_URI'],
);
}
public function getDatabaseEnvironment(): DatabaseEnvironment
{
return new DatabaseEnvironment(
$_SERVER['DB_DRIVER'],
$_SERVER['DB_PATH']
);
}
public function isProduction(): bool {
return $_SERVER['PRODUCTION'] === 'true';
}
<?php
declare(strict_types=1);
namespace GamesShop\Environment;
use DotenvVault\DotenvVault;
use GamesShop\Paths;
final class EnvironmentHandler
{
private const string ENVIRONMENT_PATH = Paths::ROOT_PATH . '/config';
public function load() {
$dotEnv = DotenvVault::createImmutable(
self::ENVIRONMENT_PATH
);
$dotEnv->safeLoad();
}
public function getDiscordEnvironment(): DiscordEnvironment {
return new DiscordEnvironment(
$_SERVER['DISCORD_CLIENT_ID'],
$_SERVER['DISCORD_CLIENT_SECRET'],
$_SERVER['DISCORD_CLIENT_LOGIN_URI'],
);
}
public function getDatabaseEnvironment(): DatabaseEnvironment
{
return new DatabaseEnvironment(
$_SERVER['DB_DRIVER'],
$_SERVER['DB_PATH']
);
}
public function isProduction(): bool {
return $_SERVER['PRODUCTION'] === 'true';
}
}

View file

@ -1,26 +1,26 @@
<?php
declare(strict_types=1);
namespace GamesShop\Errors;
use Exception;
use GamesShop\ContainerHandler;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
final class ExtendedException extends Exception
{
public function __construct(string $message = "", array $additionals = [], int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$handler = ContainerHandler::get(HandlerInterface::class);
if (!($handler instanceof PrettyPageHandler)) {
return;
}
$handler->addDataTable("Additional Info", $additionals);
}
<?php
declare(strict_types=1);
namespace GamesShop\Errors;
use Exception;
use GamesShop\ContainerHandler;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
final class ExtendedException extends Exception
{
public function __construct(string $message = "", array $additionals = [], int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$handler = ContainerHandler::get(HandlerInterface::class);
if (!($handler instanceof PrettyPageHandler)) {
return;
}
$handler->addDataTable("Additional Info", $additionals);
}
}

View file

@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
use Whoops\Run;
final class WhoopsHandler
{
<?php
declare(strict_types=1);
use Whoops\Run;
final class WhoopsHandler
{
}

View file

@ -1,166 +1,166 @@
<?php
declare(strict_types=1);
namespace GamesShop\Importer;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\Games\Game;
use GamesShop\Entities\Games\Key;
use GamesShop\Entities\Games\KeyAttribute;
use GamesShop\Entities\Games\Store;
use GamesShop\Entities\GamesList;
use PhpOffice\PhpSpreadsheet\IOFactory;
final class GameImporter
{
private const HEADER_ROW_INDEX = 1;
private const STORE_ADDITIONAL_CASES = [
'epic' => Store::EPICGAMES,
'ea' => Store::ORIGIN,
'eaplay' => Store::ORIGIN,
'ubisoft' => Store::UPLAY,
'activision' => Store::BATTLENET
];
public function __construct(
private readonly EntityManagerInterface $entityManager,
) { }
/**
* @param string $path
* @return ImportColumnInterpretation[]
*/
public function interpret(string $path): array {
$spreadsheet = IOFactory::load($path);
$worksheet = $spreadsheet->getSheet(0);
$result = [];
foreach ($worksheet->getColumnIterator() as $column) {
$columnIndex = $column->getColumnIndex();
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, self::HEADER_ROW_INDEX))->getValueString();
if (empty(trim($value))) {
continue;
}
$guessedAttribute = $this->guessAttribute($value);
$result[] = new ImportColumnInterpretation(
$columnIndex,
$value,
$guessedAttribute
);
}
return $result;
}
private function guessAttribute(string $value): KeyAttribute|null {
$value = trim($value);
$value = strtolower($value);
$value = str_replace(' ', '_', $value);
$attribute = match($value) {
'key' => KeyAttribute::KEY,
'name', 'game_name', 'game' => KeyAttribute::GAME_NAME,
'from' => KeyAttribute::FROM,
'store', 'for' => KeyAttribute::STORE,
default => null
};
return $attribute;
}
/**
* @param string[] $columnDefinitions
*/
public function import(string $path, array $columnDefinitions, GamesList $list): array {
$spreadsheet = IOFactory::load($path);
$worksheet = $spreadsheet->getSheet(0);
$totalRows = 0;
$addedAmount = 0;
foreach ($worksheet->getRowIterator(self::HEADER_ROW_INDEX + 1) as $row) {
$totalRows++;
$values = [
'name' => null,
'key' => null,
'from' => null,
'store' => null,
'store_link' => null
];
foreach ($columnDefinitions as $columnIndex => $attribute) {
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, $row->getRowIndex()))->getValueString();
switch(KeyAttribute::from($attribute)) {
case KeyAttribute::NONE:
break;
case KeyAttribute::GAME_NAME:
$values['name'] = $value;
break;
case KeyAttribute::KEY:
$values['key'] = $value;
break;
case KeyAttribute::FROM:
$values['from'] = $value;
break;
case KeyAttribute::STORE:
$store = $this->interpretStore($value);
$values['store'] = $store;
if ($store === Store::EXTERNAL) {
$values['store_link'] = $value;
}
break;
}
}
if ($values['key'] === null || $values['name'] === null || $values['store'] === null) {
continue;
}
$game = $this->entityManager->getRepository(Game::class)->findOneBy([ 'name' => $values['name'] ]);
if ($game === null) {
$game = new Game($values['name']);
}
$key = new Key(
$game,
$list,
$values['key'],
$values['store'],
$values['store_link'],
$values['from'],
);
$this->entityManager->persist($game);
$this->entityManager->persist($key);
$addedAmount++;
}
$this->entityManager->flush();
return [$totalRows, $addedAmount];
}
private function interpretStore(string $storeString): Store {
$storeString = trim($storeString);
$storeString = strtolower($storeString);
$storeString = str_replace(' ', '', $storeString);
$triedConversion = Store::tryFrom($storeString);
if ($triedConversion !== null) {
return $triedConversion;
}
if (array_key_exists($storeString, self::STORE_ADDITIONAL_CASES)) {
return self::STORE_ADDITIONAL_CASES[$storeString];
}
return Store::EXTERNAL;
}
<?php
declare(strict_types=1);
namespace GamesShop\Importer;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\Games\Game;
use GamesShop\Entities\Games\Key;
use GamesShop\Entities\Games\KeyAttribute;
use GamesShop\Entities\Games\Store;
use GamesShop\Entities\GamesList;
use PhpOffice\PhpSpreadsheet\IOFactory;
final class GameImporter
{
private const HEADER_ROW_INDEX = 1;
private const STORE_ADDITIONAL_CASES = [
'epic' => Store::EPICGAMES,
'ea' => Store::ORIGIN,
'eaplay' => Store::ORIGIN,
'ubisoft' => Store::UPLAY,
'activision' => Store::BATTLENET
];
public function __construct(
private readonly EntityManagerInterface $entityManager,
) { }
/**
* @param string $path
* @return ImportColumnInterpretation[]
*/
public function interpret(string $path): array {
$spreadsheet = IOFactory::load($path);
$worksheet = $spreadsheet->getSheet(0);
$result = [];
foreach ($worksheet->getColumnIterator() as $column) {
$columnIndex = $column->getColumnIndex();
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, self::HEADER_ROW_INDEX))->getValueString();
if (empty(trim($value))) {
continue;
}
$guessedAttribute = $this->guessAttribute($value);
$result[] = new ImportColumnInterpretation(
$columnIndex,
$value,
$guessedAttribute
);
}
return $result;
}
private function guessAttribute(string $value): KeyAttribute|null {
$value = trim($value);
$value = strtolower($value);
$value = str_replace(' ', '_', $value);
$attribute = match($value) {
'key' => KeyAttribute::KEY,
'name', 'game_name', 'game' => KeyAttribute::GAME_NAME,
'from' => KeyAttribute::FROM,
'store', 'for' => KeyAttribute::STORE,
default => null
};
return $attribute;
}
/**
* @param string[] $columnDefinitions
*/
public function import(string $path, array $columnDefinitions, GamesList $list): array {
$spreadsheet = IOFactory::load($path);
$worksheet = $spreadsheet->getSheet(0);
$totalRows = 0;
$addedAmount = 0;
foreach ($worksheet->getRowIterator(self::HEADER_ROW_INDEX + 1) as $row) {
$totalRows++;
$values = [
'name' => null,
'key' => null,
'from' => null,
'store' => null,
'store_link' => null
];
foreach ($columnDefinitions as $columnIndex => $attribute) {
$value = $worksheet->getCell(sprintf('%s%d', $columnIndex, $row->getRowIndex()))->getValueString();
switch(KeyAttribute::from($attribute)) {
case KeyAttribute::NONE:
break;
case KeyAttribute::GAME_NAME:
$values['name'] = $value;
break;
case KeyAttribute::KEY:
$values['key'] = $value;
break;
case KeyAttribute::FROM:
$values['from'] = $value;
break;
case KeyAttribute::STORE:
$store = $this->interpretStore($value);
$values['store'] = $store;
if ($store === Store::EXTERNAL) {
$values['store_link'] = $value;
}
break;
}
}
if ($values['key'] === null || $values['name'] === null || $values['store'] === null) {
continue;
}
$game = $this->entityManager->getRepository(Game::class)->findOneBy([ 'name' => $values['name'] ]);
if ($game === null) {
$game = new Game($values['name']);
}
$key = new Key(
$game,
$list,
$values['key'],
$values['store'],
$values['store_link'],
$values['from'],
);
$this->entityManager->persist($game);
$this->entityManager->persist($key);
$addedAmount++;
}
$this->entityManager->flush();
return [$totalRows, $addedAmount];
}
private function interpretStore(string $storeString): Store {
$storeString = trim($storeString);
$storeString = strtolower($storeString);
$storeString = str_replace(' ', '', $storeString);
$triedConversion = Store::tryFrom($storeString);
if ($triedConversion !== null) {
return $triedConversion;
}
if (array_key_exists($storeString, self::STORE_ADDITIONAL_CASES)) {
return self::STORE_ADDITIONAL_CASES[$storeString];
}
return Store::EXTERNAL;
}
}

View file

@ -1,26 +1,26 @@
<?php
declare(strict_types=1);
namespace GamesShop\Importer;
use GamesShop\Entities\Games\KeyAttribute;
use JsonSerializable;
final readonly class ImportColumnInterpretation implements JsonSerializable
{
public function __construct(
public string $index,
public string $displayName,
public KeyAttribute|null $guessedAttribute
)
{ }
public function jsonSerialize(): array
{
return [
'index' => $this->index,
'displayName' => $this->displayName,
'guessedAttribute' => $this->guessedAttribute ?? null
];
}
<?php
declare(strict_types=1);
namespace GamesShop\Importer;
use GamesShop\Entities\Games\KeyAttribute;
use JsonSerializable;
final readonly class ImportColumnInterpretation implements JsonSerializable
{
public function __construct(
public string $index,
public string $displayName,
public KeyAttribute|null $guessedAttribute
)
{ }
public function jsonSerialize(): array
{
return [
'index' => $this->index,
'displayName' => $this->displayName,
'guessedAttribute' => $this->guessedAttribute ?? null
];
}
}

View file

@ -1,47 +1,47 @@
<?php
declare(strict_types=1);
namespace GamesShop\Login;
use Doctrine\ORM\EntityManager;
use GamesShop\Api\DiscordAPI;
use GamesShop\ContainerHandler;
use GamesShop\Entities\Account\User;
use Psr\Http\Message\ServerRequestInterface;
final class DiscordLoginProvider implements LoginProvider
{
public function __construct(
private readonly EntityManager $entityManager
)
{
}
public function getUser(ServerRequestInterface $request): User
{
$discordApiHandler = ContainerHandler::get(DiscordAPI::class);
$result = $discordApiHandler->getUserFromCode($request->getQueryParams()['code'], (string)$request->getUri()->withQuery(''));
$repo = $this->entityManager->getRepository(User::class);
$users = $repo->findBy(['loginMethod' => LoginMethod::DISCORD, 'foreignLoginId' => $result['id']]);
$profilePictureUrl = $discordApiHandler->getAvatarURL($result['id'], $result['avatar'] ?? $result['discriminator'] % 5);
if (!empty($users)) {
$user = $users[0];
$user->setName($result['global_name']);
$user->setProfilePictureUrl($profilePictureUrl);
return $user;
}
$newUser = new User(
LoginMethod::DISCORD,
$result['id'],
$result['global_name'],
$profilePictureUrl,
UserPermission::VIEWER
);
return $newUser;
}
<?php
declare(strict_types=1);
namespace GamesShop\Login;
use Doctrine\ORM\EntityManager;
use GamesShop\Api\DiscordAPI;
use GamesShop\ContainerHandler;
use GamesShop\Entities\Account\User;
use Psr\Http\Message\ServerRequestInterface;
final class DiscordLoginProvider implements LoginProvider
{
public function __construct(
private readonly EntityManager $entityManager
)
{
}
public function getUser(ServerRequestInterface $request): User
{
$discordApiHandler = ContainerHandler::get(DiscordAPI::class);
$result = $discordApiHandler->getUserFromCode($request->getQueryParams()['code'], (string)$request->getUri()->withQuery(''));
$repo = $this->entityManager->getRepository(User::class);
$users = $repo->findBy(['loginMethod' => LoginMethod::DISCORD, 'foreignLoginId' => $result['id']]);
$profilePictureUrl = $discordApiHandler->getAvatarURL($result['id'], $result['avatar'] ?? $result['discriminator'] % 5);
if (!empty($users)) {
$user = $users[0];
$user->setName($result['global_name']);
$user->setProfilePictureUrl($profilePictureUrl);
return $user;
}
$newUser = new User(
LoginMethod::DISCORD,
$result['id'],
$result['global_name'],
$profilePictureUrl,
UserPermission::VIEWER
);
return $newUser;
}
}

View file

@ -1,76 +1,76 @@
<?php
declare(strict_types=1);
namespace GamesShop\Login;
use Doctrine\ORM\EntityManager;
use Exception;
use GamesShop\ContainerHandler;
use GamesShop\Entities\Account\User;
final class LoginHandler
{
/**
* @return class-string[]
*/
private static array $providers;
public function __construct(
private readonly EntityManager $entityManager
)
{
}
public function isLoggedIn(): bool {
$this->ensureSession();
return isset($_SESSION['accountid']);
}
/**
* @throws Exception
*/
public function getLoginProvider(string $method): LoginProvider {
$providers = self::getProviders();
if (!array_key_exists($method, $providers)) {
throw new Exception("Couldn't find method for login '{$method}'");
}
return ContainerHandler::get($providers[$method]);
}
public function setCurrentUser(User $user) {
$this->ensureSession();
$_SESSION['accountid'] = $user->getId();
}
public function getCurrentUser(): User {
$this->ensureSession();
$userid = $_SESSION['accountid'];
return $this->entityManager->getRepository(User::class)->find($userid);
}
public function deleteSession() {
$this->ensureSession();
session_destroy();
}
private function ensureSession()
{
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
}
/**
* @return class-string[]
*/
private static function getProviders(): array
{
return self::$providers ??= [
'discord' => DiscordLoginProvider::class
];
}
<?php
declare(strict_types=1);
namespace GamesShop\Login;
use Doctrine\ORM\EntityManager;
use Exception;
use GamesShop\ContainerHandler;
use GamesShop\Entities\Account\User;
final class LoginHandler
{
/**
* @return class-string[]
*/
private static array $providers;
public function __construct(
private readonly EntityManager $entityManager
)
{
}
public function isLoggedIn(): bool {
$this->ensureSession();
return isset($_SESSION['accountid']);
}
/**
* @throws Exception
*/
public function getLoginProvider(string $method): LoginProvider {
$providers = self::getProviders();
if (!array_key_exists($method, $providers)) {
throw new Exception("Couldn't find method for login '{$method}'");
}
return ContainerHandler::get($providers[$method]);
}
public function setCurrentUser(User $user) {
$this->ensureSession();
$_SESSION['accountid'] = $user->getId();
}
public function getCurrentUser(): User {
$this->ensureSession();
$userid = $_SESSION['accountid'];
return $this->entityManager->getRepository(User::class)->find($userid);
}
public function deleteSession() {
$this->ensureSession();
session_destroy();
}
private function ensureSession()
{
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
}
/**
* @return class-string[]
*/
private static function getProviders(): array
{
return self::$providers ??= [
'discord' => DiscordLoginProvider::class
];
}
}

View file

@ -1,23 +1,23 @@
<?php
declare(strict_types=1);
namespace GamesShop\Login;
enum LoginMethod: int
{
case DISCORD = 1;
public function getIconClass(): string
{
return match ($this) {
self::DISCORD => 'fa-discord',
};
}
public function getHumanReadableName(): string
{
return match ($this) {
self::DISCORD => 'Discord',
};
}
}
<?php
declare(strict_types=1);
namespace GamesShop\Login;
enum LoginMethod: int
{
case DISCORD = 1;
public function getIconClass(): string
{
return match ($this) {
self::DISCORD => 'fa-discord',
};
}
public function getHumanReadableName(): string
{
return match ($this) {
self::DISCORD => 'Discord',
};
}
}

View file

@ -1,12 +1,12 @@
<?php
declare(strict_types=1);
namespace GamesShop\Login;
use GamesShop\Entities\Account\User;
use Psr\Http\Message\ServerRequestInterface;
interface LoginProvider
{
public function getUser(ServerRequestInterface $request): User;
<?php
declare(strict_types=1);
namespace GamesShop\Login;
use GamesShop\Entities\Account\User;
use Psr\Http\Message\ServerRequestInterface;
interface LoginProvider
{
public function getUser(ServerRequestInterface $request): User;
}

View file

@ -1,25 +1,25 @@
<?php
declare(strict_types=1);
namespace GamesShop\Login;
enum UserPermission : int
{
case NONE = 0;
case VIEWER = 1;
case PROVIDER = 10;
case ADMIN = 100;
public function hasLevel(UserPermission $userPermission): bool {
return $this->value >= $userPermission->value;
}
public function getHumanReadableName() {
return match ($this) {
self::VIEWER => "Claimer",
self::PROVIDER => "Provider",
self::ADMIN => "Admin",
default => "None",
};
}
}
<?php
declare(strict_types=1);
namespace GamesShop\Login;
enum UserPermission : int
{
case NONE = 0;
case VIEWER = 1;
case PROVIDER = 10;
case ADMIN = 100;
public function hasLevel(UserPermission $userPermission): bool {
return $this->value >= $userPermission->value;
}
public function getHumanReadableName() {
return match ($this) {
self::VIEWER => "Claimer",
self::PROVIDER => "Provider",
self::ADMIN => "Admin",
default => "None",
};
}
}

View file

@ -1,13 +1,13 @@
<?php
declare(strict_types=1);
namespace GamesShop;
final class Paths
{
public const string ROOT_PATH = __DIR__ . '/../..';
public const string PUBLIC_PATH = self::ROOT_PATH . '/public';
public const string SOURCE_PATH = self::ROOT_PATH . '/src';
public const string PHP_SOURCE_PATH = self::SOURCE_PATH . '/php';
<?php
declare(strict_types=1);
namespace GamesShop;
final class Paths
{
public const string ROOT_PATH = __DIR__ . '/../..';
public const string PUBLIC_PATH = self::ROOT_PATH . '/public';
public const string SOURCE_PATH = self::ROOT_PATH . '/src';
public const string PHP_SOURCE_PATH = self::SOURCE_PATH . '/php';
}

View file

@ -1,49 +1,49 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use GamesShop\Routing\Responses\TemplateResponse;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\Diactoros\Response\RedirectResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class AdminAccountConfigRoute
{
public function __construct(
private readonly LoginHandler $loginHandler
)
{
}
/**
* @throws ForbiddenException
* @throws UnauthorizedException
*/
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if ($user->getPermission()->value < UserPermission::ADMIN->value) {
throw new ForbiddenException();
}
return new TemplateResponse('admin/accounts');
}
public static function applyRoutes(\League\Route\Router $router) {
$router->get('/accounts', self::class);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use GamesShop\Routing\Responses\TemplateResponse;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\Diactoros\Response\RedirectResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class AdminAccountConfigRoute
{
public function __construct(
private readonly LoginHandler $loginHandler
)
{
}
/**
* @throws ForbiddenException
* @throws UnauthorizedException
*/
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if ($user->getPermission()->value < UserPermission::ADMIN->value) {
throw new ForbiddenException();
}
return new TemplateResponse('admin/accounts');
}
public static function applyRoutes(\League\Route\Router $router) {
$router->get('/accounts', self::class);
}
}

View file

@ -1,16 +1,16 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api;
use GamesShop\Routing\Api\DataTables\DataTablesAPIRoutes;
use GamesShop\Routing\Api\Web\WebAPIRoutes;
use League\Route\Router;
final class APIRoutes
{
public static function applyRoutes(Router $router) {
$router->group('/api/dt', DataTablesAPIRoutes::setupRoutes(...));
$router->group('/api/web', WebAPIRoutes::applyRoutes(...));
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api;
use GamesShop\Routing\Api\DataTables\DataTablesAPIRoutes;
use GamesShop\Routing\Api\Web\WebAPIRoutes;
use League\Route\Router;
final class APIRoutes
{
public static function applyRoutes(Router $router) {
$router->group('/api/dt', DataTablesAPIRoutes::setupRoutes(...));
$router->group('/api/web', WebAPIRoutes::applyRoutes(...));
}
}

View file

@ -1,80 +1,80 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\DataTables;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\Account\User;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use League\Route\RouteGroup;
use League\Route\Router;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class AccountsEndpoint
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager,
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::ADMIN)) {
throw new ForbiddenException();
}
$params = $request->getQueryParams();
$draw = $params['draw'];
$start = $params['start'];
$length = $params['length'];
$searchValue = $params['search']['value'];
$repo = $this->entityManager->getRepository(User::class);
$total = $repo->count();
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->contains('name', $searchValue));
$criteria->setFirstResult((int)$start);
$criteria->setMaxResults((int)$length);
$values = $repo->matching($criteria);
$filteredCount = $values->count();
return new JsonResponse([
'draw' => $draw,
'recordsTotal' => $total,
'recordsFiltered' => $filteredCount,
'data' =>
$values->map(function (User $user) {
return [
'userid' => $user->getId(),
'name' => $user->getName(),
'profilePictureUrl' => $user->getProfilePictureUrl(),
'permission' => $user->getPermission()->getHumanReadableName(),
'permissionIndex' => $user->getPermission()->value,
'loginMethod' => $user->getLoginMethod()->getHumanReadableName(),
];
})->toArray()
]);
}
public static function applyRoutes(RouteGroup $router) {
$router->get('/accounts', AccountsEndpoint::class);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\DataTables;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\Account\User;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use League\Route\RouteGroup;
use League\Route\Router;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class AccountsEndpoint
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager,
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::ADMIN)) {
throw new ForbiddenException();
}
$params = $request->getQueryParams();
$draw = $params['draw'];
$start = $params['start'];
$length = $params['length'];
$searchValue = $params['search']['value'];
$repo = $this->entityManager->getRepository(User::class);
$total = $repo->count();
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->contains('name', $searchValue));
$criteria->setFirstResult((int)$start);
$criteria->setMaxResults((int)$length);
$values = $repo->matching($criteria);
$filteredCount = $values->count();
return new JsonResponse([
'draw' => $draw,
'recordsTotal' => $total,
'recordsFiltered' => $filteredCount,
'data' =>
$values->map(function (User $user) {
return [
'userid' => $user->getId(),
'name' => $user->getName(),
'profilePictureUrl' => $user->getProfilePictureUrl(),
'permission' => $user->getPermission()->getHumanReadableName(),
'permissionIndex' => $user->getPermission()->value,
'loginMethod' => $user->getLoginMethod()->getHumanReadableName(),
];
})->toArray()
]);
}
public static function applyRoutes(RouteGroup $router) {
$router->get('/accounts', AccountsEndpoint::class);
}
}

View file

@ -1,16 +1,16 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\DataTables;
use League\Route\RouteGroup;
use League\Route\Router;
final class DataTablesAPIRoutes
{
public static function setupRoutes(RouteGroup $group): void {
AccountsEndpoint::applyRoutes($group);
$group->get('/keys/provider', ProviderKeysEndpoint::class);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\DataTables;
use League\Route\RouteGroup;
use League\Route\Router;
final class DataTablesAPIRoutes
{
public static function setupRoutes(RouteGroup $group): void {
AccountsEndpoint::applyRoutes($group);
$group->get('/keys/provider', ProviderKeysEndpoint::class);
}
}

View file

@ -1,78 +1,78 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\DataTables;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\Games\Game;
use GamesShop\Entities\Games\Key;
use GamesShop\Entities\GamesList;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ProviderKeysEndpoint
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager,
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$body = $request->getQueryParams();
if (!array_key_exists('listid', $body)) {
throw new BadRequestException();
}
$list = $this->entityManager->getRepository(GamesList::class)->findOneBy([ 'owner' => $user, 'id' => $body['listid'] ]);
if (!$list instanceof GamesList) {
throw new BadRequestException();
}
$keys = $this->entityManager->getRepository(Key::class)->findBy(['list' => $list]);
$gameToKeyArray = [];
foreach ($keys as $key) {
$game = $key->getGame();
$id = $game->getId();
if (!array_key_exists($id, $gameToKeyArray)) {
$gameToKeyArray[$id] = [ $game, [] ];
}
$gameToKeyArray[$id][1][] = $key;
}
$result = [];
foreach ($gameToKeyArray as [$game, $keys]) {
$result[] = [
'gamePicture' => '',
'name' => $game->getName(),
'keysAmount' => count($keys),
'igdbState' => 'not implermented',
'keys' => $keys,
];
}
return new JsonResponse([ 'data' => $result ]);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\DataTables;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\Games\Game;
use GamesShop\Entities\Games\Key;
use GamesShop\Entities\GamesList;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ProviderKeysEndpoint
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager,
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$body = $request->getQueryParams();
if (!array_key_exists('listid', $body)) {
throw new BadRequestException();
}
$list = $this->entityManager->getRepository(GamesList::class)->findOneBy([ 'owner' => $user, 'id' => $body['listid'] ]);
if (!$list instanceof GamesList) {
throw new BadRequestException();
}
$keys = $this->entityManager->getRepository(Key::class)->findBy(['list' => $list]);
$gameToKeyArray = [];
foreach ($keys as $key) {
$game = $key->getGame();
$id = $game->getId();
if (!array_key_exists($id, $gameToKeyArray)) {
$gameToKeyArray[$id] = [ $game, [] ];
}
$gameToKeyArray[$id][1][] = $key;
}
$result = [];
foreach ($gameToKeyArray as [$game, $keys]) {
$result[] = [
'gamePicture' => '',
'name' => $game->getName(),
'keysAmount' => count($keys),
'igdbState' => 'not implermented',
'keys' => $keys,
];
}
return new JsonResponse([ 'data' => $result ]);
}
}

View file

@ -1,49 +1,49 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\GamesList;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class CreateKeyListRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$body = $request->getParsedBody();
if (!array_key_exists('name', $body)) {
throw new BadRequestException();
}
$name = $body['name'];
$list = new GamesList($user, $name);
$this->entityManager->persist($list);
$this->entityManager->flush();
return new Response();
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\GamesList;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class CreateKeyListRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$body = $request->getParsedBody();
if (!array_key_exists('name', $body)) {
throw new BadRequestException();
}
$name = $body['name'];
$list = new GamesList($user, $name);
$this->entityManager->persist($list);
$this->entityManager->flush();
return new Response();
}
}

View file

@ -1,50 +1,50 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use GamesShop\Importer\GameImporter;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\UploadedFile;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ImportKeysPrepareRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly GameImporter $importer,
) { }
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
/**
* @var UploadedFile $file
*/
$file = $request->getUploadedFiles()['file'] ?? null;
if (!$file === null) {
throw new BadRequestException();
}
$fileName = tempnam(sys_get_temp_dir(), 'ImportKeys');
$file->moveTo($fileName);
$results = $this->importer->interpret($fileName);
unlink($fileName);
return new JsonResponse($results);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use GamesShop\Importer\GameImporter;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\UploadedFile;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ImportKeysPrepareRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly GameImporter $importer,
) { }
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
/**
* @var UploadedFile $file
*/
$file = $request->getUploadedFiles()['file'] ?? null;
if (!$file === null) {
throw new BadRequestException();
}
$fileName = tempnam(sys_get_temp_dir(), 'ImportKeys');
$file->moveTo($fileName);
$results = $this->importer->interpret($fileName);
unlink($fileName);
return new JsonResponse($results);
}
}

View file

@ -1,66 +1,66 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\GamesList;
use GamesShop\Importer\GameImporter;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\UploadedFile;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ImportKeysRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly GameImporter $importer,
private readonly EntityManager $entityManager,
) { }
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$body = $request->getParsedBody();
if (!array_key_exists('listid', $body)) {
throw new BadRequestException();
}
$list = $this->entityManager->getRepository(GamesList::class)->findOneBy([ 'owner' => $user, 'id' => $body['listid'] ]);
if (!$list instanceof GamesList) {
throw new BadRequestException();
}
/**
* @var UploadedFile $file
*/
$file = $request->getUploadedFiles()['file'] ?? null;
if (!$file === null) {
throw new BadRequestException();
}
$fileName = tempnam(sys_get_temp_dir(), 'ImportKeys');
$file->moveTo($fileName);
$columnDefs = $request->getParsedBody()['columns'];
[$total, $imported] = $this->importer->import($fileName, $columnDefs, $list);
unlink($fileName);
return new JsonResponse([ 'success' => true, 'total' => $total, 'imported' => $imported ]);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\GamesList;
use GamesShop\Importer\GameImporter;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\UploadedFile;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ImportKeysRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly GameImporter $importer,
private readonly EntityManager $entityManager,
) { }
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$body = $request->getParsedBody();
if (!array_key_exists('listid', $body)) {
throw new BadRequestException();
}
$list = $this->entityManager->getRepository(GamesList::class)->findOneBy([ 'owner' => $user, 'id' => $body['listid'] ]);
if (!$list instanceof GamesList) {
throw new BadRequestException();
}
/**
* @var UploadedFile $file
*/
$file = $request->getUploadedFiles()['file'] ?? null;
if (!$file === null) {
throw new BadRequestException();
}
$fileName = tempnam(sys_get_temp_dir(), 'ImportKeys');
$file->moveTo($fileName);
$columnDefs = $request->getParsedBody()['columns'];
[$total, $imported] = $this->importer->import($fileName, $columnDefs, $list);
unlink($fileName);
return new JsonResponse([ 'success' => true, 'total' => $total, 'imported' => $imported ]);
}
}

View file

@ -1,46 +1,46 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use GamesShop\Entities\Account\User;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final readonly class UserModifyRoute
{
public function __construct(
private LoginHandler $loginHandler,
private EntityManager $entityManager,
)
{
}
public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::ADMIN)) {
throw new ForbiddenException();
}
$permissions = $request->getParsedBody()['permission'];
$toChangeUser = $this->entityManager->getRepository(User::class)->find((int)$args['id']);
$toChangeUser->setPermission(UserPermission::from((int)$permissions));
$this->entityManager->flush();
return new EmptyResponse(200);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use GamesShop\Entities\Account\User;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final readonly class UserModifyRoute
{
public function __construct(
private LoginHandler $loginHandler,
private EntityManager $entityManager,
)
{
}
public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::ADMIN)) {
throw new ForbiddenException();
}
$permissions = $request->getParsedBody()['permission'];
$toChangeUser = $this->entityManager->getRepository(User::class)->find((int)$args['id']);
$toChangeUser->setPermission(UserPermission::from((int)$permissions));
$this->entityManager->flush();
return new EmptyResponse(200);
}
}

View file

@ -1,18 +1,18 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use League\Route\RouteGroup;
final class WebAPIRoutes
{
public static function applyRoutes(RouteGroup $group): void {
$group->post('/users/{id:number}', UserModifyRoute::class);
$group->post('/keys/import/prepare', ImportKeysPrepareRoute::class);
$group->post('/keys/import/perform', ImportKeysRoute::class);
$group->post('/keys/list/create', CreateKeyListRoute::class);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\Web;
use League\Route\RouteGroup;
final class WebAPIRoutes
{
public static function applyRoutes(RouteGroup $group): void {
$group->post('/users/{id:number}', UserModifyRoute::class);
$group->post('/keys/import/prepare', ImportKeysPrepareRoute::class);
$group->post('/keys/import/perform', ImportKeysRoute::class);
$group->post('/keys/list/create', CreateKeyListRoute::class);
}
}

View file

@ -1,20 +1,20 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;
final class ErrorRoute
{
public function renderErrorPage(int $errorCode): ResponseInterface {
$pageContent = ContainerHandler::get(TemplateEngine::class)->renderErrorPage($errorCode);
$response = new Response(status: $errorCode);
$response->getBody()->write($pageContent);
return $response;
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;
final class ErrorRoute
{
public function renderErrorPage(int $errorCode): ResponseInterface {
$pageContent = ContainerHandler::get(TemplateEngine::class)->renderErrorPage($errorCode);
$response = new Response(status: $errorCode);
$response->getBody()->write($pageContent);
return $response;
}
}

View file

@ -1,33 +1,33 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class IndexRoute
{
public function __invoke(ServerRequestInterface $request): ResponseInterface {
$loginHandler = ContainerHandler::get(LoginHandler::class);
if (!$loginHandler->isLoggedIn()) {
return new RedirectResponse('/login');
}
$pageContent = ContainerHandler::get(TemplateEngine::class)->renderPage('index');
$response = new Response;
$response->getBody()->write($pageContent);
return $response;
}
public static function applyRoutes(\League\Route\Router $router): void {
$router->get('/', self::class);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class IndexRoute
{
public function __invoke(ServerRequestInterface $request): ResponseInterface {
$loginHandler = ContainerHandler::get(LoginHandler::class);
if (!$loginHandler->isLoggedIn()) {
return new RedirectResponse('/login');
}
$pageContent = ContainerHandler::get(TemplateEngine::class)->renderPage('index');
$response = new Response;
$response->getBody()->write($pageContent);
return $response;
}
public static function applyRoutes(\League\Route\Router $router): void {
$router->get('/', self::class);
}
}

View file

@ -1,45 +1,45 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\GamesList;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use GamesShop\Routing\Responses\TemplateResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class KeysRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$entityManager = $this->entityManager->getRepository(GamesList::class);
$lists = $entityManager->findBy([ 'owner' => $user ]);
return new TemplateResponse('key-manager', [ 'usersLists' => $lists ]);
}
public static function applyRoutes(\League\Route\Router $router): void
{
$router->get('/keys', KeysRoute::class);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\GamesList;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use GamesShop\Routing\Responses\TemplateResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class KeysRoute
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$entityManager = $this->entityManager->getRepository(GamesList::class);
$lists = $entityManager->findBy([ 'owner' => $user ]);
return new TemplateResponse('key-manager', [ 'usersLists' => $lists ]);
}
public static function applyRoutes(\League\Route\Router $router): void
{
$router->get('/keys', KeysRoute::class);
}
}

View file

@ -1,72 +1,72 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use GamesShop\ContainerHandler;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Routing\Responses\TemplateResponse;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class LoginRoutes
{
public function __construct(
private readonly EntityManager $entityManager
)
{
}
public function login(ServerRequestInterface $request) {
$discordEnv = ContainerHandler::get(EnvironmentHandler::class)->getDiscordEnvironment();
return new TemplateResponse('login', [
'discordUrl' => $discordEnv->loginUrl
]);
}
/**
* @throws OptimisticLockException
* @throws ORMException
*/
public function loginCallback(ServerRequestInterface $request, array $args): ResponseInterface {
if (array_key_exists('error', $request->getQueryParams())) {
return new Response\RedirectResponse('/login');
}
$method = $args['method'];
$loginHandler = ContainerHandler::get(LoginHandler::class);
$loginProvider = $loginHandler->getLoginProvider($method);
$user = $loginProvider->getUser($request);
if ($user->getId() === null) {
$this->entityManager->persist($user);
$this->entityManager->flush();
}
$loginHandler->setCurrentUser($user);
return new Response\RedirectResponse('/');
}
public function logout(ServerRequestInterface $request): ResponseInterface
{
$loginHandler = ContainerHandler::get(LoginHandler::class);
$loginHandler->deleteSession();
return new Response\RedirectResponse('/login');
}
public static function addRoutes(\League\Route\Router $router): void {
$routes = ContainerHandler::get(LoginRoutes::class);
$router->get('/login', $routes->login(...));
$router->get('/login-callback/{method:word}', $routes->loginCallback(...));
$router->get('/logout', $routes->logout(...));
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use GamesShop\ContainerHandler;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Routing\Responses\TemplateResponse;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class LoginRoutes
{
public function __construct(
private readonly EntityManager $entityManager
)
{
}
public function login(ServerRequestInterface $request) {
$discordEnv = ContainerHandler::get(EnvironmentHandler::class)->getDiscordEnvironment();
return new TemplateResponse('login', [
'discordUrl' => $discordEnv->loginUrl
]);
}
/**
* @throws OptimisticLockException
* @throws ORMException
*/
public function loginCallback(ServerRequestInterface $request, array $args): ResponseInterface {
if (array_key_exists('error', $request->getQueryParams())) {
return new Response\RedirectResponse('/login');
}
$method = $args['method'];
$loginHandler = ContainerHandler::get(LoginHandler::class);
$loginProvider = $loginHandler->getLoginProvider($method);
$user = $loginProvider->getUser($request);
if ($user->getId() === null) {
$this->entityManager->persist($user);
$this->entityManager->flush();
}
$loginHandler->setCurrentUser($user);
return new Response\RedirectResponse('/');
}
public function logout(ServerRequestInterface $request): ResponseInterface
{
$loginHandler = ContainerHandler::get(LoginHandler::class);
$loginHandler->deleteSession();
return new Response\RedirectResponse('/login');
}
public static function addRoutes(\League\Route\Router $router): void {
$routes = ContainerHandler::get(LoginRoutes::class);
$router->get('/login', $routes->login(...));
$router->get('/login-callback/{method:word}', $routes->loginCallback(...));
$router->get('/logout', $routes->logout(...));
}
}

View file

@ -1,55 +1,55 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Paths;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Uri;
use Mimey\MimeTypes;
use Psr\Http\Message\ResponseInterface;
final class ResourceRoute
{
private const array RESOURCE_EXTENSIONS = [
'js',
'css',
'ttf', 'woff', 'woff2',
'gif', 'svg', 'png', 'jpg'
];
public function isValid(Uri $uri): bool {
$path = $uri->getPath();
foreach (self::RESOURCE_EXTENSIONS as $extension) {
if (!str_ends_with($path, $extension)) {
continue;
}
return true;
}
return false;
}
public function getResponse(Uri $uri): ResponseInterface {
$filePath = Paths::PUBLIC_PATH . $uri->getPath();
if (!file_exists($filePath)) {
$response = new Response(status: 404);
$response->getBody()->write('File not found');
return $response;
}
$mimey = ContainerHandler::get(MimeTypes::class);
$response = new Response(
headers: [
'Content-Type' => $mimey->getMimeType(pathinfo($filePath, PATHINFO_EXTENSION)),
'Cache-Control' => 'public, max-age=3600, must-revalidate',
]
);
$response->getBody()->write(file_get_contents($filePath));
return $response;
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Paths;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Uri;
use Mimey\MimeTypes;
use Psr\Http\Message\ResponseInterface;
final class ResourceRoute
{
private const array RESOURCE_EXTENSIONS = [
'js',
'css',
'ttf', 'woff', 'woff2',
'gif', 'svg', 'png', 'jpg'
];
public function isValid(Uri $uri): bool {
$path = $uri->getPath();
foreach (self::RESOURCE_EXTENSIONS as $extension) {
if (!str_ends_with($path, $extension)) {
continue;
}
return true;
}
return false;
}
public function getResponse(Uri $uri): ResponseInterface {
$filePath = Paths::PUBLIC_PATH . $uri->getPath();
if (!file_exists($filePath)) {
$response = new Response(status: 404);
$response->getBody()->write('File not found');
return $response;
}
$mimey = ContainerHandler::get(MimeTypes::class);
$response = new Response(
headers: [
'Content-Type' => $mimey->getMimeType(pathinfo($filePath, PATHINFO_EXTENSION)),
'Cache-Control' => 'public, max-age=3600, must-revalidate',
]
);
$response->getBody()->write(file_get_contents($filePath));
return $response;
}
}

View file

@ -1,20 +1,20 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Responses;
use GamesShop\ContainerHandler;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
final class TemplateResponse extends Response
{
public function __construct(string $templateName, array $data = [], array $headers = [])
{
parent::__construct('php://memory', 200, $headers);
$templateEngine = ContainerHandler::get(TemplateEngine::class);
$body = $templateEngine->renderPage($templateName, $data);
$this->getBody()->write($body);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Responses;
use GamesShop\ContainerHandler;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
final class TemplateResponse extends Response
{
public function __construct(string $templateName, array $data = [], array $headers = [])
{
parent::__construct('php://memory', 200, $headers);
$templateEngine = ContainerHandler::get(TemplateEngine::class);
$body = $templateEngine->renderPage($templateName, $data);
$this->getBody()->write($body);
}
}

View file

@ -1,61 +1,61 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Routing\Api\APIRoutes;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use League\Container\Container;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\NotFoundException;
use League\Route\Http\Exception\UnauthorizedException;
use League\Route\Strategy\ApplicationStrategy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class Router
{
public function __construct(
private ResourceRoute $resourceRoute
)
{
}
public function route(): ResponseInterface
{
$request = ServerRequestFactory::fromGlobals(
$_SERVER, $_GET, $_POST, $_COOKIE, $_FILES
);
if ($this->resourceRoute->isValid($request->getUri())) {
return $this->resourceRoute->getResponse($request->getUri());
}
$router = new \League\Route\Router;
$strategy = (new ApplicationStrategy)->setContainer(ContainerHandler::getInstance());
$router->setStrategy($strategy);
IndexRoute::applyRoutes($router);
LoginRoutes::addRoutes($router);
SetupRoute::applyRoutes($router);
KeysRoute::applyRoutes($router);
AdminAccountConfigRoute::applyRoutes($router);
APIRoutes::applyRoutes($router);
try {
return $router->dispatch($request);
} catch (NotFoundException $e) {
return (new ErrorRoute())->renderErrorPage(404);
} catch (UnauthorizedException) {
return (new ErrorRoute())->renderErrorPage(401);
} catch (ForbiddenException) {
return (new ErrorRoute())->renderErrorPage(403);
}
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use GamesShop\ContainerHandler;
use GamesShop\Login\LoginHandler;
use GamesShop\Routing\Api\APIRoutes;
use GamesShop\Templates\TemplateEngine;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use League\Container\Container;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\NotFoundException;
use League\Route\Http\Exception\UnauthorizedException;
use League\Route\Strategy\ApplicationStrategy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class Router
{
public function __construct(
private ResourceRoute $resourceRoute
)
{
}
public function route(): ResponseInterface
{
$request = ServerRequestFactory::fromGlobals(
$_SERVER, $_GET, $_POST, $_COOKIE, $_FILES
);
if ($this->resourceRoute->isValid($request->getUri())) {
return $this->resourceRoute->getResponse($request->getUri());
}
$router = new \League\Route\Router;
$strategy = (new ApplicationStrategy)->setContainer(ContainerHandler::getInstance());
$router->setStrategy($strategy);
IndexRoute::applyRoutes($router);
LoginRoutes::addRoutes($router);
SetupRoute::applyRoutes($router);
KeysRoute::applyRoutes($router);
AdminAccountConfigRoute::applyRoutes($router);
APIRoutes::applyRoutes($router);
try {
return $router->dispatch($request);
} catch (NotFoundException $e) {
return (new ErrorRoute())->renderErrorPage(404);
} catch (UnauthorizedException) {
return (new ErrorRoute())->renderErrorPage(401);
} catch (ForbiddenException) {
return (new ErrorRoute())->renderErrorPage(403);
}
}
}

View file

@ -1,52 +1,52 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\SystemAttribute;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\RedirectResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class SetupRoute
{
public function __construct(
private readonly EntityManager $entityManager,
private readonly LoginHandler $loginHandler
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface {
if (!$this->loginHandler->isLoggedIn()) {
return new RedirectResponse('/login');
}
$repo = $this->entityManager->getRepository(SystemAttribute::class);
$attribute = $repo->find('ADMIN_SETUP_COMPLETED');
if ($attribute) {
return new RedirectResponse('/');
}
$user = $this->loginHandler->getCurrentUser();
$user->setPermission(UserPermission::ADMIN);
$attribute = new SystemAttribute(
'ADMIN_SETUP_COMPLETED',
'true'
);
$this->entityManager->persist($attribute);
$this->entityManager->flush();
return new RedirectResponse('/');
}
public static function applyRoutes(\League\Route\Router $router) {
$router->get('/setup-admin', self::class);
}
<?php
declare(strict_types=1);
namespace GamesShop\Routing;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\SystemAttribute;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\RedirectResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class SetupRoute
{
public function __construct(
private readonly EntityManager $entityManager,
private readonly LoginHandler $loginHandler
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface {
if (!$this->loginHandler->isLoggedIn()) {
return new RedirectResponse('/login');
}
$repo = $this->entityManager->getRepository(SystemAttribute::class);
$attribute = $repo->find('ADMIN_SETUP_COMPLETED');
if ($attribute) {
return new RedirectResponse('/');
}
$user = $this->loginHandler->getCurrentUser();
$user->setPermission(UserPermission::ADMIN);
$attribute = new SystemAttribute(
'ADMIN_SETUP_COMPLETED',
'true'
);
$this->entityManager->persist($attribute);
$this->entityManager->flush();
return new RedirectResponse('/');
}
public static function applyRoutes(\League\Route\Router $router) {
$router->get('/setup-admin', self::class);
}
}

View file

@ -1,13 +1,13 @@
<?php
declare(strict_types=1);
namespace GamesShop;
final class SetupHandler
{
public function __construct(
)
{
}
<?php
declare(strict_types=1);
namespace GamesShop;
final class SetupHandler
{
public function __construct(
)
{
}
}

View file

@ -1,17 +1,17 @@
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use GamesShop\Login\UserPermission;
final class NavigationHeader
{
public function __construct(
public readonly string $title,
public readonly string $link,
public readonly UserPermission $minimumPermission,
)
{
}
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use GamesShop\Login\UserPermission;
final class NavigationHeader
{
public function __construct(
public readonly string $title,
public readonly string $link,
public readonly UserPermission $minimumPermission,
)
{
}
}

View file

@ -1,16 +1,16 @@
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
final class ResourceEntry
{
/**
* @param string[] $js
* @param string[] $css
*/
public function __construct(
public readonly array $js,
public readonly array $css,
) { }
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
final class ResourceEntry
{
/**
* @param string[] $js
* @param string[] $css
*/
public function __construct(
public readonly array $js,
public readonly array $css,
) { }
}

View file

@ -1,51 +1,51 @@
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use Exception;
use GamesShop\Paths;
final class ResourceIndex
{
private const string PATH = Paths::SOURCE_PATH . '/file-index.json';
/**
* @var ResourceEntry[]
*/
private array $resources = [];
public function __construct()
{
$fileContents = file_get_contents(self::PATH);
$index = json_decode($fileContents, true);
foreach ($index as $entryKey => $resource) {
$js = $resource['js'];
if (is_string($js)) {
$js = [$js];
}
$css = $resource['css'];
if (is_string($css)) {
$css = [$css];
}
$this->resources[$entryKey] = new ResourceEntry(
$js, $css
);
}
}
/**
* @throws Exception
*/
public function getResource(string $entry): ResourceEntry
{
if (!array_key_exists($entry, $this->resources)) {
throw new Exception("Entry '$entry' not found");
}
return $this->resources[$entry];
}
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use Exception;
use GamesShop\Paths;
final class ResourceIndex
{
private const string PATH = Paths::SOURCE_PATH . '/file-index.json';
/**
* @var ResourceEntry[]
*/
private array $resources = [];
public function __construct()
{
$fileContents = file_get_contents(self::PATH);
$index = json_decode($fileContents, true);
foreach ($index as $entryKey => $resource) {
$js = $resource['js'];
if (is_string($js)) {
$js = [$js];
}
$css = $resource['css'];
if (is_string($css)) {
$css = [$css];
}
$this->resources[$entryKey] = new ResourceEntry(
$js, $css
);
}
}
/**
* @throws Exception
*/
public function getResource(string $entry): ResourceEntry
{
if (!array_key_exists($entry, $this->resources)) {
throw new Exception("Entry '$entry' not found");
}
return $this->resources[$entry];
}
}

View file

@ -1,34 +1,34 @@
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use GamesShop\Login\LoginHandler;
use GamesShop\Paths;
use League\Plates\Engine;
final class TemplateEngine extends Engine
{
private const string TEMPLATES_PATH = Paths::SOURCE_PATH . '/templates';
public function __construct(
private ResourceIndex $resourceIndex,
LoginHandler $loginHandler,
)
{
parent::__construct(self::TEMPLATES_PATH, 'php');
$this->addData([
'resources' => $this->resourceIndex,
'activeUser' => $loginHandler->isLoggedIn() ? $loginHandler->getCurrentUser() : null,
]);
}
public function renderPage(string $page, array $data = array())
{
return parent::render("pages/$page", $data);
}
public function renderErrorPage(int $error) {
return self::renderPage('error', [ 'errorCode' => $error ]);
}
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use GamesShop\Login\LoginHandler;
use GamesShop\Paths;
use League\Plates\Engine;
final class TemplateEngine extends Engine
{
private const string TEMPLATES_PATH = Paths::SOURCE_PATH . '/templates';
public function __construct(
private ResourceIndex $resourceIndex,
LoginHandler $loginHandler,
)
{
parent::__construct(self::TEMPLATES_PATH, 'php');
$this->addData([
'resources' => $this->resourceIndex,
'activeUser' => $loginHandler->isLoggedIn() ? $loginHandler->getCurrentUser() : null,
]);
}
public function renderPage(string $page, array $data = array())
{
return parent::render("pages/$page", $data);
}
public function renderErrorPage(int $error) {
return self::renderPage('error', [ 'errorCode' => $error ]);
}
}

View file

@ -1,15 +1,15 @@
#!/usr/bin/env php
<?php
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
use GamesShop\ContainerHandler;
require_once __DIR__ . '/../bootstrap.php';
$entityManager = ContainerHandler::get(EntityManager::class);
ConsoleRunner::run(
new SingleManagerProvider($entityManager)
#!/usr/bin/env php
<?php
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
use GamesShop\ContainerHandler;
require_once __DIR__ . '/../bootstrap.php';
$entityManager = ContainerHandler::get(EntityManager::class);
ConsoleRunner::run(
new SingleManagerProvider($entityManager)
);

View file

@ -1,11 +1,11 @@
<?php
declare(strict_types=1);
use GamesShop\ContainerHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
require_once __DIR__ . '/../../vendor/autoload.php';
ContainerHandler::get(EnvironmentHandler::class)->load();
<?php
declare(strict_types=1);
use GamesShop\ContainerHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
require_once __DIR__ . '/../../vendor/autoload.php';
ContainerHandler::get(EnvironmentHandler::class)->load();
ContainerHandler::get(DoctrineManager::class)->setup();

View file

@ -1,25 +1,25 @@
<?php
declare(strict_types=1);
use GamesShop\ContainerHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Routing\Router;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
require_once __DIR__ . '/../src/php/bootstrap.php';
$whoops = new Run();
$prettyPageHandler = new PrettyPageHandler();
$whoops->pushHandler($prettyPageHandler);
$whoops->register();
ContainerHandler::getInstance()->addShared(HandlerInterface::class, $prettyPageHandler);
$router = ContainerHandler::getInstance()->get(Router::class);
$result = $router->route();
<?php
declare(strict_types=1);
use GamesShop\ContainerHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Routing\Router;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
require_once __DIR__ . '/../src/php/bootstrap.php';
$whoops = new Run();
$prettyPageHandler = new PrettyPageHandler();
$whoops->pushHandler($prettyPageHandler);
$whoops->register();
ContainerHandler::getInstance()->addShared(HandlerInterface::class, $prettyPageHandler);
$router = ContainerHandler::getInstance()->get(Router::class);
$result = $router->route();
(new SapiEmitter)->emit($result);

View file

@ -1,2 +1,2 @@
<?php
<?php
declare(strict_types=1);

View file

@ -1,35 +1,35 @@
<?php
declare(strict_types=1);
use GamesShop\Entities\Account\User;
/** @var User|null $activeUser */
?>
<?php if ($activeUser !== null): ?>
<div class="d-flex avatar justify-content-center">
<div class="avatar-icon h-100 position-relative me-2 ratio-1">
<img src="<?= $activeUser->getProfilePictureUrl(); ?>" class="rounded-circle h-100" alt="User Profile Picture" />
<div class="position-absolute bottom-0 end-0 ratio-1 d-flex align-items-center z-1 avatar-login-method">
<i class="fa-brands <?= $activeUser->getLoginMethod()->getIconClass() ?>"></i>
<span class="position-absolute w-100 h-100 bottom-0 end-0 ratio-1 bg-body rounded-circle z-n1 avatar-login-method-icon"></span>
</div>
</div>
<div class="d-flex flex-column">
<span class="me-2 h-3">
<?= $activeUser->getName() ?>
</span>
<small class="text-muted">
<?= $activeUser->getPermission()->getHumanReadableName() ?>
</small>
</div>
<div class="h-100 d-flex align-items-center ms-2">
<a href="/logout">
<i class="fa-solid fa-arrow-right-to-bracket fa-xl text-danger"></i>
</a>
</div>
</div>
<?php endif ?>
<?php
declare(strict_types=1);
use GamesShop\Entities\Account\User;
/** @var User|null $activeUser */
?>
<?php if ($activeUser !== null): ?>
<div class="d-flex avatar justify-content-center">
<div class="avatar-icon h-100 position-relative me-2 ratio-1">
<img src="<?= $activeUser->getProfilePictureUrl(); ?>" class="rounded-circle h-100" alt="User Profile Picture" />
<div class="position-absolute bottom-0 end-0 ratio-1 d-flex align-items-center z-1 avatar-login-method">
<i class="fa-brands <?= $activeUser->getLoginMethod()->getIconClass() ?>"></i>
<span class="position-absolute w-100 h-100 bottom-0 end-0 ratio-1 bg-body rounded-circle z-n1 avatar-login-method-icon"></span>
</div>
</div>
<div class="d-flex flex-column">
<span class="me-2 h-3">
<?= $activeUser->getName() ?>
</span>
<small class="text-muted">
<?= $activeUser->getPermission()->getHumanReadableName() ?>
</small>
</div>
<div class="h-100 d-flex align-items-center ms-2">
<a href="/logout">
<i class="fa-solid fa-arrow-right-to-bracket fa-xl text-danger"></i>
</a>
</div>
</div>
<?php endif ?>

View file

@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
?>
<?php
declare(strict_types=1);
?>

View file

@ -1,49 +1,49 @@
<?php
declare(strict_types=1);
use GamesShop\Templates\ResourceIndex;
use League\Plates\Template\Template;
assert($this instanceof Template);
/** @var ResourceIndex $resources */
assert($resources instanceof ResourceIndex);
if (!isset($resourceEntry)) {
throw new Exception("Resource entry not set");
}
/**
* @var string $resourceEntry
*/
$resource = $resources->getResource($resourceEntry);
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Game Shop</title>
<?php foreach ($resource->js as $js): ?>
<script src="/<?= $js ?>"></script>
<?php endforeach; ?>
<?php foreach ($resource->css as $css): ?>
<link rel="stylesheet" href="/<?= $css ?>">
<?php endforeach; ?>
</head>
<body class="vh-100 d-flex flex-column">
<?= $this->insert('layout/navbar') ?>
<main class="mt-2 position-relative flex-grow-1">
<?= $this->section('content'); ?>
</main>
<?= $this->section('modal') ?>
<div class="position-absolute bottom-0 start-50 opacity-25 text-center translate-middle-x">
<span class="h1">PROTOTYPE / POC</span>
</div>
</body>
</html>
<?php
declare(strict_types=1);
use GamesShop\Templates\ResourceIndex;
use League\Plates\Template\Template;
assert($this instanceof Template);
/** @var ResourceIndex $resources */
assert($resources instanceof ResourceIndex);
if (!isset($resourceEntry)) {
throw new Exception("Resource entry not set");
}
/**
* @var string $resourceEntry
*/
$resource = $resources->getResource($resourceEntry);
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Game Shop</title>
<?php foreach ($resource->js as $js): ?>
<script src="/<?= $js ?>"></script>
<?php endforeach; ?>
<?php foreach ($resource->css as $css): ?>
<link rel="stylesheet" href="/<?= $css ?>">
<?php endforeach; ?>
</head>
<body class="vh-100 d-flex flex-column">
<?= $this->insert('layout/navbar') ?>
<main class="mt-2 position-relative flex-grow-1">
<?= $this->section('content'); ?>
</main>
<?= $this->section('modal') ?>
<div class="position-absolute bottom-0 start-50 opacity-25 text-center translate-middle-x">
<span class="h1">PROTOTYPE / POC</span>
</div>
</body>
</html>

View file

@ -1,53 +1,53 @@
<?php
declare(strict_types=1);
use GamesShop\Entities\Account\User;
use GamesShop\Login\UserPermission;
use GamesShop\Templates\NavigationHeader;
$headers = [
new NavigationHeader('My Keys', '/keys', UserPermission::PROVIDER),
new NavigationHeader('Accounts', '/accounts', UserPermission::ADMIN)
];
/** @var User|null $activeUser */
$currentPermission = $activeUser === null ? UserPermission::NONE : $activeUser->getPermission();
?>
<nav class="navbar navbar-expand-lg bg-body-tertiary main-navigation">
<div class="container-fluid navigation-container">
<a class="navbar-brand" href="/">Game Shop</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-content" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar-content">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<?php foreach ($headers as $header):
if (!$currentPermission->hasLevel($header->minimumPermission)) {
continue;
}
?>
<li class="nav-link">
<a href="<?= $header->link ?>" class="nav-link"><?= $header->title ?></a>
</li>
<?php endforeach; ?>
</ul>
<?= $this->insert('layout/accountDisplay'); ?>
</div>
</div>
<div class="d-flex mode-switch me-3">
<button title="Use dark mode" id="dark" class="btn btn-sm btn-default text-secondary">
<i class="fa-regular fa-moon"></i>
</button>
<button title="Use light mode" id="light" class="btn btn-sm btn-default text-secondary">
<i class="fa-regular fa-sun"></i>
</button>
<button title="Use system preferred mode" id="system" class="btn btn-sm btn-default text-secondary">
<i class="fa-solid fa-display"></i>
</button>
</div>
<?php
declare(strict_types=1);
use GamesShop\Entities\Account\User;
use GamesShop\Login\UserPermission;
use GamesShop\Templates\NavigationHeader;
$headers = [
new NavigationHeader('My Keys', '/keys', UserPermission::PROVIDER),
new NavigationHeader('Accounts', '/accounts', UserPermission::ADMIN)
];
/** @var User|null $activeUser */
$currentPermission = $activeUser === null ? UserPermission::NONE : $activeUser->getPermission();
?>
<nav class="navbar navbar-expand-lg bg-body-tertiary main-navigation">
<div class="container-fluid navigation-container">
<a class="navbar-brand" href="/">Game Shop</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-content" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar-content">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<?php foreach ($headers as $header):
if (!$currentPermission->hasLevel($header->minimumPermission)) {
continue;
}
?>
<li class="nav-link">
<a href="<?= $header->link ?>" class="nav-link"><?= $header->title ?></a>
</li>
<?php endforeach; ?>
</ul>
<?= $this->insert('layout/accountDisplay'); ?>
</div>
</div>
<div class="d-flex mode-switch me-3">
<button title="Use dark mode" id="dark" class="btn btn-sm btn-default text-secondary">
<i class="fa-regular fa-moon"></i>
</button>
<button title="Use light mode" id="light" class="btn btn-sm btn-default text-secondary">
<i class="fa-regular fa-sun"></i>
</button>
<button title="Use system preferred mode" id="system" class="btn btn-sm btn-default text-secondary">
<i class="fa-solid fa-display"></i>
</button>
</div>
</nav>

View file

@ -1,57 +1,57 @@
<?php
declare(strict_types=1);
use GamesShop\Login\UserPermission;
$this->layout('layout/main', [ 'resourceEntry' => 'admin/accounts' ]);
?>
<h1>Users</h1>
<table id="user-table" class="table table-striped w-100">
<thead>
<tr>
<th width="2.4rem"></th>
<th>Name</th>
<th>Permission</th>
<th>Login-Method</th>
</tr>
</thead>
<tbody></tbody>
</table>
<?php $this->start('modal') ?>
<div class="modal" id="edit-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title h3">
Edit User
</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-2">
Name: <span class="name-input"></span>
</div>
<div class="mb-2">
Login Method: <span class="login-method"></span>
</div>
<div>
<label for="permissions">Permissions:</label>
<select name="" id="permissions" class="form-select permission-editor">
<?php foreach (UserPermission::cases() as $userPermission):?>
<option value="<?= $userPermission->value ?>"><?= $userPermission->getHumanReadableName() ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary js--save">Save changes</button>
</div>
</div>
</div>
</div>
<?php $this->end() ?>
<?php
declare(strict_types=1);
use GamesShop\Login\UserPermission;
$this->layout('layout/main', [ 'resourceEntry' => 'admin/accounts' ]);
?>
<h1>Users</h1>
<table id="user-table" class="table table-striped w-100">
<thead>
<tr>
<th width="2.4rem"></th>
<th>Name</th>
<th>Permission</th>
<th>Login-Method</th>
</tr>
</thead>
<tbody></tbody>
</table>
<?php $this->start('modal') ?>
<div class="modal" id="edit-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title h3">
Edit User
</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-2">
Name: <span class="name-input"></span>
</div>
<div class="mb-2">
Login Method: <span class="login-method"></span>
</div>
<div>
<label for="permissions">Permissions:</label>
<select name="" id="permissions" class="form-select permission-editor">
<?php foreach (UserPermission::cases() as $userPermission):?>
<option value="<?= $userPermission->value ?>"><?= $userPermission->getHumanReadableName() ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary js--save">Save changes</button>
</div>
</div>
</div>
</div>
<?php $this->end() ?>

View file

@ -1,11 +1,11 @@
<?php
declare(strict_types=1);
$this->layout('layout/main', [ 'resourceEntry' => 'index' ]);
/**
* @var int $errorCode
*/
?>
<img src="https://http.dog/<?= $errorCode ?>.jpg" alt="Error <?= $errorCode ?>" class="position-absolute top-50 start-50 translate-middle w-100" style="max-width: 800px" />
<?php
declare(strict_types=1);
$this->layout('layout/main', [ 'resourceEntry' => 'index' ]);
/**
* @var int $errorCode
*/
?>
<img src="https://http.dog/<?= $errorCode ?>.jpg" alt="Error <?= $errorCode ?>" class="position-absolute top-50 start-50 translate-middle w-100" style="max-width: 800px" />

View file

@ -1,9 +1,9 @@
<?php
declare(strict_types=1);
$this->layout('layout/main', [ 'resourceEntry' => 'index' ]);
?>
<h1>
Hello
<?php
declare(strict_types=1);
$this->layout('layout/main', [ 'resourceEntry' => 'index' ]);
?>
<h1>
Hello
</h1>

View file

@ -1,143 +1,153 @@
<?php
declare(strict_types=1);
use GamesShop\Entities\Games\KeyAttribute;
use GamesShop\Entities\GamesList;
use League\Plates\Template\Template;
assert($this instanceof Template);
/** @var GamesList[] $usersLists */
$this->layout('layout/main', [ 'resourceEntry' => 'keys' ]);
?>
<meta name="key-attributes" content="<?= htmlspecialchars(json_encode(KeyAttribute::casesAsAssociative())) ?>" />
<div class="row">
<div class="col-sm-6">
<h1>My Keys</h1>
</div>
<div class="col-sm-6 align-self-center">
<?php if (!empty($usersLists)): ?>
<select name="lists" id="list-select" class="form-select w-100">
<?php foreach ($usersLists as $list): ?>
<option value="<?= $list->getId() ?>"><?= $list->getName() ?></option>
<?php endforeach; ?>
<option value="_create">+ Create New</option>
</select>
<?php endif; ?>
</div>
</div>
<?php if (empty($usersLists)): ?>
<div class="text-center">
<p class="fs-4 mb-4">You don't have a key list. Create one here.</p>
<button class="btn btn-primary btn-lg js--create-list-button">
Create
</button>
</div>
<?php else: ?>
<ul id="key-tab" class="nav nav-tabs">
<li class="nav-item">
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#keys-tab-pane" role="tab">
Keys
</button>
</li>
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#import-tab-pane" role="tab">
Import
</button>
</li>
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#share-tab-pane" role="tab">
Share
</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="keys-tab-pane" role="tabpanel">
<table class="table table-striped key-table">
<thead>
<tr>
<td></td>
<td>Game Name</td>
<td>Amount Keys</td>
<td>IGDB State</td>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="tab-pane fade" id="import-tab-pane" role="tabpanel">
<h2>Importer</h2>
<label for="formFile" class="form-label mt-3">Insert import file:</label>
<div class="mb-3 input-group">
<input class="form-control" type="file" id="import" accept="text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
<button class="btn btn-primary js--send-import">Send</button>
</div>
<div class="d-none" id="import-info-container">
<h3>Import Details:</h3>
<table class="table table-striped w-100" id="import-attribute-table">
<thead>
<tr>
<td>Column</td>
<td>Header</td>
<td>Attribute</td>
</tr>
</thead>
<tbody></tbody>
</table>
<button class="btn btn-primary js--do-import">
Import
</button>
</div>
</div>
<div class="tab-pane fade" id="share-tab-pane" role="tabpanel">
<h2>
Share your list
</h2>
<label for="share-user-search">Search for a user...</label>
<div class="input-group">
<input type="text" class="form-control" id="share-user-search" placeholder="">
<button class="btn btn-primary js--search-shared-user">Search</button>
</div>
</div>
</div>
<?php endif; ?>
<?php $this->start('modal') ?>
<div class="modal" id="create-list-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title h3">
Create list
</h1>
</div>
<div class="modal-body">
<div class="form-floating">
<input class="form-control" type="text" id="createListName" placeholder="">
<label for="createListName">Name</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary js--create-list">Create</button>
</div>
</div>
</div>
</div>
<?php $this->end() ?>
<?php
declare(strict_types=1);
use GamesShop\Entities\Games\KeyAttribute;
use GamesShop\Entities\GamesList;
use League\Plates\Template\Template;
assert($this instanceof Template);
/** @var GamesList[] $usersLists */
$this->layout('layout/main', [ 'resourceEntry' => 'keys' ]);
?>
<meta name="key-attributes" content="<?= htmlspecialchars(json_encode(KeyAttribute::casesAsAssociative())) ?>" />
<div class="row">
<div class="col-sm-6">
<h1>My Keys</h1>
</div>
<div class="col-sm-6 align-self-center">
<?php if (!empty($usersLists)): ?>
<select name="lists" id="list-select" class="form-select w-100">
<?php foreach ($usersLists as $list): ?>
<option value="<?= $list->getId() ?>"><?= $list->getName() ?></option>
<?php endforeach; ?>
<option value="_create">+ Create New</option>
</select>
<?php endif; ?>
</div>
</div>
<?php if (empty($usersLists)): ?>
<div class="text-center">
<p class="fs-4 mb-4">You don't have a key list. Create one here.</p>
<button class="btn btn-primary btn-lg js--create-list-button">
Create
</button>
</div>
<?php else: ?>
<ul id="key-tab" class="nav nav-tabs">
<li class="nav-item">
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#keys-tab-pane" role="tab">
Keys
</button>
</li>
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#import-tab-pane" role="tab">
Import
</button>
</li>
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#share-tab-pane" role="tab">
Share
</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="keys-tab-pane" role="tabpanel">
<table class="table table-striped key-table">
<thead>
<tr>
<th></th>
<th>Game Name</th>
<th>Amount Keys</th>
<th>IGDB State</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="tab-pane fade" id="import-tab-pane" role="tabpanel">
<h2>Importer</h2>
<label for="formFile" class="form-label mt-3">Insert import file:</label>
<div class="mb-3 input-group">
<input class="form-control" type="file" id="import" accept="text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
<button class="btn btn-primary js--send-import">Send</button>
</div>
<div class="d-none" id="import-info-container">
<h3>Import Details:</h3>
<table class="table table-striped w-100" id="import-attribute-table">
<thead>
<tr>
<th>Column</th>
<th>Header</th>
<th>Attribute</th>
</tr>
</thead>
<tbody></tbody>
</table>
<button class="btn btn-primary js--do-import">
Import
</button>
</div>
</div>
<div class="tab-pane fade" id="share-tab-pane" role="tabpanel">
<h2>
Share your list
</h2>
<label for="share-user-search">Search for a user...</label>
<div class="input-group mb-3">
<input type="text" class="form-control" id="share-user-search" placeholder="">
<button class="btn btn-primary js--search-shared-user">Search</button>
</div>
<h3>Users shared to</h3>
<table id="shared-users-table" class="table table-striped w-100">
<thead>
<tr>
<th width="2.4rem"></th>
<th>Name</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<?php endif; ?>
<?php $this->start('modal') ?>
<div class="modal" id="create-list-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title h3">
Create list
</h1>
</div>
<div class="modal-body">
<div class="form-floating">
<input class="form-control" type="text" id="createListName" placeholder="">
<label for="createListName">Name</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary js--create-list">Create</button>
</div>
</div>
</div>
</div>
<?php $this->end() ?>

View file

@ -1,15 +1,15 @@
<?php
declare(strict_types=1);
$this->layout('layout/main', [ 'resourceEntry' => 'index' ]);
/** @var string $discordUrl */
?>
<a href="<?= $discordUrl ?>">
<button class="btn btn-primary position-absolute top-50 start-50 translate-middle">
<i class="fa-brands fa-discord"></i>
Login with Discord
</button>
<?php
declare(strict_types=1);
$this->layout('layout/main', [ 'resourceEntry' => 'index' ]);
/** @var string $discordUrl */
?>
<a href="<?= $discordUrl ?>">
<button class="btn btn-primary position-absolute top-50 start-50 translate-middle">
<i class="fa-brands fa-discord"></i>
Login with Discord
</button>
</a>

View file

@ -1,10 +1,10 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

View file

@ -1,102 +1,102 @@
const MiniCssExtractPlugin = require('mini-css-extract-plugin'),
Path = require('path'),
AssetsPlugin = require('assets-webpack-plugin'),
CopyPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const PUBLIC_FOLDER = Path.resolve(__dirname, 'public'),
SOURCE_FOLDER = Path.resolve(__dirname, 'src'),
JS_FOLDER = Path.resolve(SOURCE_FOLDER, 'js'),
CSS_FOLDER = Path.resolve(SOURCE_FOLDER, 'css'),
PHP_FOLDER = Path.resolve(SOURCE_FOLDER, 'php');
const PROD = false;
const INDEX_PATH = Path.resolve(PHP_FOLDER, PROD ? 'index.prod.php' : 'index.dev.php');
module.exports = {
plugins: [
new AssetsPlugin({
filename: 'file-index.json',
path: SOURCE_FOLDER,
includeAllFileTypes: false,
entrypoints: true,
removeFullPathAutoPrefix: true
}),
new MiniCssExtractPlugin({
filename: 'css/[name].css'
}),
new CopyPlugin({
patterns: [
{
from: INDEX_PATH,
to: PUBLIC_FOLDER + '/index.php'
},
]
})
],
mode: PROD ? 'production' : 'development',
devtool: 'source-map',
optimization: {
runtimeChunk: "single",
minimize: true,
minimizer: [
new CssMinimizerPlugin()
],
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
common: {
test: /[\\/]common[\\/]/,
name: 'common',
chunks: 'all'
}
},
},
},
module: {
rules: [
{
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
],
},
{
test: /\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'resolve-url-loader',
"sass-loader"
]
},
{
test: /\.ts$/i,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(woff|woff2|eot|ttf|otf|gif|svg)$/i,
type: 'asset/resource',
}
],
},
resolve: {
extensions: ['.js', '.ts'],
},
entry: {
index: JS_FOLDER + "/pages/index",
'admin/accounts': JS_FOLDER + "/pages/admin/accounts",
keys: JS_FOLDER + "/pages/keys/index",
},
output: {
path: PUBLIC_FOLDER,
filename: 'js/[name].js'
}
const MiniCssExtractPlugin = require('mini-css-extract-plugin'),
Path = require('path'),
AssetsPlugin = require('assets-webpack-plugin'),
CopyPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const PUBLIC_FOLDER = Path.resolve(__dirname, 'public'),
SOURCE_FOLDER = Path.resolve(__dirname, 'src'),
JS_FOLDER = Path.resolve(SOURCE_FOLDER, 'js'),
CSS_FOLDER = Path.resolve(SOURCE_FOLDER, 'css'),
PHP_FOLDER = Path.resolve(SOURCE_FOLDER, 'php');
const PROD = false;
const INDEX_PATH = Path.resolve(PHP_FOLDER, PROD ? 'index.prod.php' : 'index.dev.php');
module.exports = {
plugins: [
new AssetsPlugin({
filename: 'file-index.json',
path: SOURCE_FOLDER,
includeAllFileTypes: false,
entrypoints: true,
removeFullPathAutoPrefix: true
}),
new MiniCssExtractPlugin({
filename: 'css/[name].css'
}),
new CopyPlugin({
patterns: [
{
from: INDEX_PATH,
to: PUBLIC_FOLDER + '/index.php'
},
]
})
],
mode: PROD ? 'production' : 'development',
devtool: 'source-map',
optimization: {
runtimeChunk: "single",
minimize: true,
minimizer: [
new CssMinimizerPlugin()
],
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
common: {
test: /[\\/]common[\\/]/,
name: 'common',
chunks: 'all'
}
},
},
},
module: {
rules: [
{
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
],
},
{
test: /\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'resolve-url-loader',
"sass-loader"
]
},
{
test: /\.ts$/i,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(woff|woff2|eot|ttf|otf|gif|svg)$/i,
type: 'asset/resource',
}
],
},
resolve: {
extensions: ['.js', '.ts'],
},
entry: {
index: JS_FOLDER + "/pages/index",
'admin/accounts': JS_FOLDER + "/pages/admin/accounts",
keys: JS_FOLDER + "/pages/keys/index",
},
output: {
path: PUBLIC_FOLDER,
filename: 'js/[name].js'
}
}