Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
2019_TP2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nicolas.paschoud
2019_TP2
Commits
5a62c5f3
Commit
5a62c5f3
authored
5 years ago
by
noe.fleury
Browse files
Options
Downloads
Patches
Plain Diff
Added sharing part
parent
9ce35bd6
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
projet/db/data.sql
+1
-1
1 addition, 1 deletion
projet/db/data.sql
projet/db/hyperdrive.sql
+6
-3
6 additions, 3 deletions
projet/db/hyperdrive.sql
projet/hyperdrive-rest.js
+51
-16
51 additions, 16 deletions
projet/hyperdrive-rest.js
projet/sql-request.js
+48
-1
48 additions, 1 deletion
projet/sql-request.js
with
106 additions
and
21 deletions
projet/db/data.sql
+
1
−
1
View file @
5a62c5f3
This diff is collapsed.
Click to expand it.
projet/db/hyperdrive.sql
+
6
−
3
View file @
5a62c5f3
...
...
@@ -47,7 +47,8 @@ VALUES
(
"b"
,
"test"
),
(
"c"
,
"test"
),
(
"d"
,
"test"
),
(
"e"
,
"test"
);
(
"e"
,
"test"
),
(
"noe"
,
"prov"
);
INSERT
INTO
Paths
VALUES
...
...
@@ -57,7 +58,8 @@ VALUES
(
"/c"
,
"c"
,
NULL
),
(
"/c/test"
,
"c"
,
"/c"
),
(
"/d"
,
"d"
,
NULL
),
(
"/e"
,
"e"
,
NULL
);
(
"/e"
,
"e"
,
NULL
),
(
"/n"
,
"noe"
,
NULL
);
INSERT
INTO
Files
VALUES
...
...
@@ -66,7 +68,8 @@ VALUES
(
"@dfsg"
,
"trois"
,
"/c/test"
,
46
.
2054
,
6
.
1459
),
(
"gbvaf"
,
"quatre"
,
"/b"
,
46
.
2054
,
6
.
1459
),
(
"dsfgh"
,
"cinq"
,
"/d"
,
46
.
2054
,
6
.
1459
),
(
"sdfa"
,
"six"
,
"/e"
,
46
.
2054
,
6
.
1459
);
(
"sdfa"
,
"six"
,
"/e"
,
46
.
2054
,
6
.
1459
),
(
"liblb"
,
"myfile"
,
"/n"
,
46
.
2054
,
6
.
1459
);
INSERT
INTO
Shares
VALUES
...
...
This diff is collapsed.
Click to expand it.
projet/hyperdrive-rest.js
+
51
−
16
View file @
5a62c5f3
...
...
@@ -54,31 +54,47 @@ function JWT(pl_user, pl_pass){
}
function
add_token
(
token
)
{
function
add_token
(
token
,
user
)
{
if
(
valid_tokens
.
indexOf
(
token
)
===
-
1
)
{
valid_tokens
.
push
(
token
.
toString
());
for
(
let
i
=
0
;
i
<
valid_tokens
.
length
;
i
++
)
{
if
(
Object
.
keys
(
valid_tokens
[
i
])[
0
]
==
token
){
valid_tokens
.
splice
(
i
,
1
);
console
.
log
(
"
Unable to add token. (token already present)
"
);
break
;
}
else
{
console
.
log
(
"
Unable to add token to valid_tokens. (token already present)
"
);
}
obj
=
{};
obj
[
token
.
toString
()]
=
user
;
valid_tokens
.
push
(
obj
);
console
.
log
(
valid_tokens
)
console
.
log
(
"
Successfully added token.
"
);
}
function
remove_token
(
token
)
{
if
(
valid_tokens
.
indexOf
(
token
)
!=
-
1
)
{
var
index
=
valid_tokens
.
indexOf
(
token
);
valid_tokens
.
splice
(
index
,
1
);
for
(
let
i
=
0
;
i
<
valid_tokens
.
length
;
i
++
)
{
if
(
Object
.
keys
(
valid_tokens
[
i
])[
0
]
==
token
){
valid_tokens
.
splice
(
i
,
1
);
console
.
log
(
"
Successfully removed token from valid_tokens.
"
);
return
true
;
}
else
{
console
.
log
(
"
Unable to remove token from valid_tokens. (Token not present)
"
);
}
console
.
log
(
"
Unable to remove token from valid_tokens. (Token not present)
"
);
}
// verify the token
// return : user if exist
function
verify_token
(
token
)
{
return
(
valid_tokens
.
indexOf
(
token
)
!==
-
1
);
for
(
let
i
=
0
;
i
<
valid_tokens
.
length
;
i
++
)
{
if
(
Object
.
keys
(
valid_tokens
[
i
])[
0
]
==
token
)
return
valid_tokens
[
i
][
token
];
}
return
false
;
}
...
...
@@ -136,7 +152,7 @@ app.get('/login', (req, res) => {
"
comment
"
:
`Password for user '
${
user
}
' true.`
})
add_token
(
jwt
.
signedToken
);
add_token
(
jwt
.
signedToken
,
user
);
}
else
{
...
...
@@ -236,8 +252,27 @@ app.get('/register/', (req, res) => {
* param : user2
* user1 share a file_id with user2
*/
app
.
get
(
'
/share/:file_id
'
,
(
req
,
res
)
=>
{
res
.
send
(
`Request for a file sharing (id:
${
req
.
params
[
'
file_id
'
]}
)`
)
app
.
get
(
'
/share/:file_id/:to_user
'
,
(
req
,
res
)
=>
{
token
=
req
.
query
[
"
token
"
]
user
=
verify_token
(
token
);
console
.
log
(
"
user :
"
+
user
)
if
(
req
.
params
[
'
to_user
'
]
&&
req
.
params
[
'
file_id
'
]){
to_user
=
req
.
params
[
'
to_user
'
];
file_id
=
req
.
params
[
'
file_id
'
];
sql
.
addSharing
(
user
,
to_user
,
file_id
).
then
(
function
(
r
)
{
res
.
send
(
r
);
})
}
else
{
res
.
send
(
"
Unable to share. Please provide a user to share with and a file_id.
"
);
}
})
/**
...
...
This diff is collapsed.
Click to expand it.
projet/sql-request.js
+
48
−
1
View file @
5a62c5f3
...
...
@@ -124,6 +124,53 @@ function changePath(login, path){
];
}
// verify if a file_id is at a user
function
verify_user_file_id
(
file_id
,
login
){
return
new
Promise
(
resolve
=>
{
let
q
=
`SELECT (login) FROM Files as F LEFT JOIN Paths as P ON F.paths = P.paths WHERE F.file_id = '
${
file_id
}
';`
con
.
query
(
q
,
function
(
err
,
res
)
{
if
(
err
)
{
console
.
log
(
"
Error while veryfing file_id for user
"
);
console
.
log
(
err
);
resolve
(
false
);
}
console
.
log
(
res
.
length
)
// return new Promise(resolve => resolve("resolved"));
if
(
res
.
length
>
0
){
if
(
res
[
0
].
login
==
login
)
resolve
(
true
);
resolve
(
false
);
}
resolve
(
false
);
});
});
}
async
function
addSharing
(
login
,
to_user
,
file_id
){
verif
=
await
verify_user_file_id
(
file_id
,
login
);
if
(
verif
){
let
q
=
`INSERT INTO Shares VALUES ('
${
login
}
', '
${
to_user
}
', '
${
file_id
}
');`
;
con
.
query
(
q
,
function
(
err
,
res
)
{
if
(
err
)
{
console
.
log
(
"
Error while adding a share path
"
);
console
.
log
(
err
);
return
"
Error sharing - (DB error)
"
;
}
});
// return "New sharing (", login, " --> " + to_user + "( - file_id : " + file_id + ")) added succesfully !";
return
"
Sharing ok.
"
}
else
{
return
"
Unable to share, this is not your file.
"
;
}
}
exports
.
userExist
=
userExist
;
exports
.
addUser
=
addUser
;
exports
.
addPath
=
addPath
;
exports
.
addSharing
=
addSharing
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment