Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Comptoir
Comptoir-srv
Commits
267009c7
Commit
267009c7
authored
Aug 10, 2021
by
Fabrice Gangler
🎨
Browse files
FEAT: add export data (Json)
parent
57fbd4f5
Pipeline
#18267
failed with stage
in 4 minutes and 59 seconds
Changes
7
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
config/routes.php
View file @
267009c7
...
...
@@ -169,8 +169,6 @@ $basicRoutes = function (RouteBuilder $routes) {
]
);
// Software ===============================================================
$routes
->
resources
(
'Softwares'
,
...
...
@@ -241,6 +239,10 @@ $basicRoutes = function (RouteBuilder $routes) {
"index/"
=>
[
'action'
=>
'index'
,
'method'
=>
[
'GET'
],
],
"export"
=>
[
'action'
=>
'export'
,
'method'
=>
[
'GET'
],
]
]
]
...
...
src/Controller/Api/V1/SoftwaresController.php
View file @
267009c7
...
...
@@ -86,6 +86,7 @@ class SoftwaresController extends AppController
if
(
$this
->
request
->
is
(
'get'
))
{
$this
->
Auth
->
allow
(
[
'export'
,
'index'
,
'add'
,
'view'
,
...
...
@@ -230,6 +231,171 @@ class SoftwaresController extends AppController
$this
->
setBreadcrumbs
();
}
/**
* Export method
*
* @return void
*/
public
function
export
()
{
$this
->
request
->
allowMethod
([
'get'
]);
////////////////////////////////////////////////////////////
// Retrieve data from database
////////////////////////////////////////////////////////////
$this
->
paginate
=
[
"Softwares"
=>
[
'sortWhitelist'
=>
[
'softwarename'
,
'average_review'
,
'created'
,
'modified'
,
],
'limit'
=>
Configure
::
read
(
'LIMIT'
),
'maxLimit'
=>
Configure
::
read
(
'LIMIT'
),
'order'
=>
[
'softwarename'
=>
Configure
::
read
(
'ORDER'
)],
'contain'
=>
[
'Reviews'
,
'Licenses'
,
'Tags'
,
'Userssoftwares'
=>
[
'strategy'
=>
'select'
,
'queryBuilder'
=>
function
(
$q
)
{
return
$q
->
order
([
'Users.username'
=>
'ASC'
]);
},
'Users'
=>
[
'fields'
=>
[
'id'
,
'username'
,
'url'
]
]
],
'Providerssoftwares'
=>
[
'strategy'
=>
'select'
,
'queryBuilder'
=>
function
(
$q
)
{
return
$q
->
order
([
'Users.username'
=>
'ASC'
]);
},
'Users'
=>
[
'fields'
=>
[
'id'
,
'username'
,
'url'
]
]
],
],
],
'contain'
=>
[
'Tags'
]
];
$softwares
=
$this
->
Softwares
->
find
(
'search'
,
[
'search'
=>
$this
->
request
->
data
,
"contain"
=>
[
"Reviews"
,
"Licenses"
],
]
)
->
select
(
[
'average_review'
=>
$this
->
Softwares
->
query
()
->
func
()
->
coalesce
(
[
$this
->
Softwares
->
query
()
->
func
()
->
avg
(
'Reviews.evaluation'
),
0
]
)
]
)
// allow to sort by the virtual property "Average"
->
leftJoinWith
(
'Reviews'
)
->
leftJoinWith
(
'Licenses'
)
->
group
([
'Softwares.id'
])
->
autoFields
(
true
);
////////////////////////////////////////////////////////////
// Generate JSON export
////////////////////////////////////////////////////////////
$data
=
[
'date_of_export'
=>
date
(
DATE_ATOM
),
'number_of_software'
=>
$softwares
->
count
(),
'softwares'
=>
[]
];
foreach
(
$this
->
paginate
(
$softwares
)
as
$software
)
{
$providers
=
[];
# @TODO bug $provider->created
foreach
(
$software
->
providerssoftwares
as
$provider
)
{
$providers
[]
=
[
'id'
=>
$provider
->
user_id
,
// 'created' => $provider->created,
// 'modified' => $provider->modified,
'url'
=>
'https://comptoir-du-libre.org/fr/users/'
.
$provider
->
user_id
,
'name'
=>
$provider
->
user
->
username
,
'type'
=>
$provider
->
user
->
user_type
->
name
,
'external_resources'
=>
[
'website'
=>
$provider
->
user
->
url
,
],
];
}
$users
=
[];
# @TODO bug $user->createdd
foreach
(
$software
->
userssoftwares
as
$user
)
{
if
(
$user
->
user
->
user_type
->
name
===
'Administration'
)
{
$users
[]
=
[
'id'
=>
$user
->
user_id
,
// 'created' => $user->created,
// 'modified' => $user->modified,
'url'
=>
'https://comptoir-du-libre.org/fr/users/'
.
$user
->
user_id
,
'name'
=>
$user
->
user
->
username
,
'type'
=>
$user
->
user
->
user_type
->
name
,
'external_resources'
=>
[
'website'
=>
$user
->
user
->
url
,
],
];
}
}
$licenceName
=
''
;
if
(
is_object
(
$software
->
license
))
{
$licenceName
=
$software
->
license
->
name
;
}
$item
=
[
'id'
=>
$software
->
id
,
'created'
=>
$software
->
created
,
'modified'
=>
$software
->
modified
,
'url'
=>
'https://comptoir-du-libre.org/fr/softwares/'
.
$software
->
id
,
'name'
=>
$software
->
softwarename
,
'licence'
=>
$licenceName
,
'external_resources'
=>
[
'website'
=>
$software
->
url_website
,
'repository'
=>
$software
->
url_repository
,
],
'providers'
=>
$providers
,
'users'
=>
$users
,
];
$data
[
'softwares'
][]
=
$item
;
}
$json
=
\
json_encode
(
$data
,
JSON_PRETTY_PRINT
);
$exportPath
=
'public/export/'
;
$exportFile
=
$exportPath
.
"comptoir-du-libre_export_v1.json"
;
if
(
is_dir
(
WWW_ROOT
.
$exportPath
))
{
file_put_contents
(
WWW_ROOT
.
$exportFile
,
"
$json
"
);
$this
->
set
(
'exportFile'
,
"
$exportFile
"
);
$this
->
set
(
'export'
,
file_get_contents
(
WWW_ROOT
.
$exportFile
));
}
}
/**
* View method
*
...
...
src/Template/Api/V1/Softwares/export.ctp
0 → 100644
View file @
267009c7
<?php
$this
->
layout
=
'base'
;
$this
->
assign
(
'title'
,
__d
(
"Softwares"
,
'Softwares.index.title'
));
// View debug json only for connected user with role = admin
if
(
$this
->
request
->
session
()
->
read
(
'Auth.User.role'
)
===
'admin'
)
{
if
(
isset
(
$export
)
&&
isset
(
$exportFile
))
{
echo
"<h1 id=
\"
export-json_displayed
\"
>JSON export</h1>"
;
echo
"<a id=
\"
link_export-json_displayed
\"
href=
\"
/
$exportFile
\"
>JSON export</a><hr>"
;
echo
"<pre>
$export
</pre><hr>"
;
}
else
{
echo
"<h1 id=
\"
error_export-json_displayed
\"
>Error: JSON export not available</h1>"
;
}
}
else
{
if
(
isset
(
$export
)
&&
isset
(
$exportFile
))
{
echo
'<div id="export-json_not-displayed"></div>'
;
}
else
{
echo
'<div id="error_export-json_not-displayed"></div>'
;
}
}
?>
tests/Acceptance/RoleAdminCheckActionsCest.php
View file @
267009c7
...
...
@@ -263,4 +263,26 @@ class RoleAdminCheckActionsCest
$I
->
seeElement
(
"//input[@id='photo']"
);
$I
->
seeElement
(
"//textarea[@id='description']"
);
}
/**
* When trying to update JSON export
* verify that we can see link to export (because we have "admin" role)
*
* @group admin
* @group security
* @group export
* @param AcceptanceTester $I codeception variable
*
* @return void
*/
public
function
displayExportJson
(
AcceptanceTester
$I
)
{
$I
->
amOnPage
(
'/softwares/export'
);
// Generate export
$I
->
seeElement
(
"//h1[@id='export-json_displayed']"
);
$I
->
seeElement
(
"//a[@id='link_export-json_displayed']"
);
$I
->
dontSeeElement
(
"//h1[@id='error_export-json_displayed']"
);
$I
->
dontSeeElement
(
"//div[@id='export-json_not-displayed']"
);
$I
->
dontSeeElement
(
"//div[@id='error_export-json_not-displayed']"
);
}
}
tests/Acceptance/UserAdministrationCheckActionsCest.php
View file @
267009c7
...
...
@@ -471,4 +471,25 @@ class UserAdministrationCheckActionsCest
$I
->
seeElement
(
'div.message.error'
);
$I
->
see
(
'You are not allowed to do that'
);
}
/**
* When trying to update JSON export
* verify that we can't see link to export (only user with "admin" role can see it)
*
* @group security
* @group export
* @param AcceptanceTester $I codeception variable
*
* @return void
*/
public
function
displayExportJson
(
AcceptanceTester
$I
)
{
$I
->
amOnPage
(
'/softwares/export'
);
// Generate export
$I
->
dontSeeElement
(
"//h1[@id='export-json_displayed']"
);
$I
->
dontSeeElement
(
"//a[@id='link_export-json_displayed']"
);
$I
->
dontSeeElement
(
"//h1[@id='error_export-json_displayed']"
);
$I
->
seeElement
(
"//div[@id='export-json_not-displayed']"
);
$I
->
dontSeeElement
(
"//div[@id='error_export-json_not-displayed']"
);
}
}
tests/Acceptance/UserAnonymousCheckActionsCest.php
View file @
267009c7
...
...
@@ -58,4 +58,25 @@ class UserAnonymousCheckActionsCest
$I
->
click
(
'//*[@id="btn_Softwares-usersSoftware-163"]'
);
// button : 'Se déclarer utilisateur'
$I
->
seeInCurrentUrl
(
"/
$lang
/users/login"
);
}
/**
* When trying to update JSON export
* verify that we can't see link to export (only user with "admin" role can see it)
*
* @group admin
* @group export
* @param AcceptanceTester $I codeception variable
*
* @return void
*/
public
function
displayExportJson
(
AcceptanceTester
$I
)
{
$I
->
amOnPage
(
'/softwares/export'
);
// Generate export
$I
->
dontSeeElement
(
"//h1[@id='export-json_displayed']"
);
$I
->
dontSeeElement
(
"//a[@id='link_export-json_displayed']"
);
$I
->
dontSeeElement
(
"//h1[@id='error_export-json_displayed']"
);
$I
->
seeElement
(
"//div[@id='export-json_not-displayed']"
);
$I
->
dontSeeElement
(
"//div[@id='error_export-json_not-displayed']"
);
}
}
webroot/public/export/index.html
0 → 100644
View file @
267009c7
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment