New route and button to anonymize old users

This commit is contained in:
nemunaire 2023-03-08 05:04:59 +01:00
parent a9bb758e99
commit 0965698c90
3 changed files with 73 additions and 5 deletions

View File

@ -19,6 +19,18 @@ export async function getUsers(promo, group) {
}
}
export async function anonOldAccounts() {
const res = await fetch('api/users', {
method: 'PATCH',
headers: {'Accept': 'application/json'},
});
if (res.status == 200) {
return await res.json()
} else {
throw new Error((await res.json()).errmsg);
}
}
export class User {
constructor(res) {
if (res) {

View File

@ -3,28 +3,45 @@
import { user } from '$lib/stores/user';
import DateFormat from '$lib/components/DateFormat.svelte';
import { getUsers, getPromos } from '$lib/users';
import { anonOldAccounts, getUsers, getPromos } from '$lib/users';
function showUser(user) {
goto(`users/${user.id}`)
}
let usersP = getUsers();
function refreshUsers() {
usersP = getUsers();
}
function askAnon() {
if (confirm("Ceci va anonymiser tous les comptes des étudiants avant " + ($user.current_promo - 1) + ". Voulez-vous continuer ?"))
anonOldAccounts().then(refreshUsers);
}
let filterPromo = "";
</script>
{#if $user && $user.is_admin}
<button
class="btn btn-danger ms-2 float-end"
on:click={askAnon}
>
<i class="bi bi-back"></i>
Anonymiser vieux comptes
</button>
<a
href="auth/gitlabcri?next={window.location.pathname}"
class="btn btn-primary float-end"
class="btn btn-primary float-end ms-2"
>
<i class="bi bi-link-45deg"></i>
OAuth GitLab
</a>
<a href="grades/{filterPromo}" class="btn btn-success me-1 float-end" title="Notes">
<a href="grades/{filterPromo}" class="btn btn-success ms-2 float-end" title="Notes">
<i class="bi bi-files"></i>
</a>
{#await getPromos() then promos}
<div class="float-end me-2">
<div class="float-end ms-2">
<select class="form-select" bind:value={filterPromo}>
<option value="">-</option>
{#each promos as promo, pid (pid)}
@ -38,7 +55,7 @@
Étudiants
</h2>
{#await getUsers()}
{#await usersP}
<div class="text-center">
<div class="spinner-border text-danger mx-3" role="status"></div>
<span>Chargement des &eacute;tudiants &hellip;</span>

View File

@ -81,6 +81,38 @@ func declareAPIAdminUsersRoutes(router *gin.RouterGroup) {
c.JSON(http.StatusOK, ret)
})
// Anonymize old accounts
router.PATCH("/users", func(c *gin.Context) {
users, err := getUsers()
if err != nil {
log.Println("Unable to getUsers:", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve users. Please try again later."})
return
}
var filterPromo *uint64
if c.Query("promo") != "" {
fPromo, err := strconv.ParseUint(c.Query("promo"), 10, 64)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Unable to parse promo: %s", err.Error())})
return
}
filterPromo = &fPromo
}
for _, u := range users {
if (filterPromo == nil && u.Promo < currentPromo-1) || (filterPromo != nil && uint(*filterPromo) == u.Promo) {
u.Anonymize()
_, err = u.Update()
if err != nil {
log.Printf("Unable to anonymize %s: %s", u.Login, err.Error())
}
}
}
c.JSON(http.StatusOK, true)
})
usersRoutes := router.Group("/users/:uid")
usersRoutes.Use(userHandler)
@ -247,6 +279,13 @@ func NewUser(login string, email string, firstname string, lastname string, prom
}
}
func (u *User) Anonymize() {
u.Login = fmt.Sprintf("Anonyme #%d", u.Id)
u.Email = fmt.Sprintf("anonyme-%d@non-existant.email", u.Id)
u.Firstname = "Arnaud"
u.Lastname = "Nimes"
}
func (u User) Update() (int64, error) {
if res, err := DBExec("UPDATE users SET login = ?, email = ?, firstname = ?, lastname = ?, time = ?, promo = ?, groups = ? WHERE id_user = ?", u.Login, u.Email, u.Firstname, u.Lastname, u.Time, u.Promo, u.Groups, u.Id); err != nil {
return 0, err