import Router from 'koa-router';
import {Authenticator} from 'middleware';

const Sector = 'Users';

const authentication = Authenticator(async (username, params, services)=>{
  const {username: userId} = params;
  return username == userId;
}, ['admin']);

const users = new Router();

users
  .get('/:username', async (ctx, next)=>{
    const {repository} = ctx.services;
    const retrievedUser = await repository.retrieve(Sector, ctx.params.username);
    if(!retrievedUser){
      ctx.status = 404;
    } else {
      const returnedUser = {};
      for(let i of retrievedUser.public){
        returnedUser[i] = retrievedUser[i];
      }
      ctx.body = returnedUser;
    }
    await next();
  })
  .get('/:username/private', authentication, async (ctx, next)=>{
    const {repository} = ctx.services;
    const retrievedUser = await repository.retrieve(Sector, ctx.params.username);
    if(!retrievedUser){
      ctx.status = 404;
    } else {
      ctx.body = retrievedUser;
    }
    await next();
  })
  .post('/', async (ctx, next)=>{
    const {repository} = ctx.services;
    const storedUser = await repository.store(Sector, ctx.request.body.username, ctx.request.body);
    if(!storedUser){
      ctx.status = 403;
    } else {
      ctx.body = {username: storedUser.username, status: true};
    }
    await next();
  })
  .put('/:username', authentication, async (ctx, next)=>{
    if(ctx.request.body.username !== ctx.params.username){
      ctx.status = 409;
    } else {
      const {repository} = ctx.services;
      const updatedUser = await repository.update(Sector, ctx.request.body.username, ctx.request.body);
      if(!updatedUser){
        ctx.status = 404;
      } else {
        ctx.body = {username: updatedUser.username, status: true};
      }
    }
    await next();
  })
  .del('/:username', authentication, async (ctx, next)=>{
    if(ctx.request.body.username !== ctx.params.username){
      ctx.status = 409;
    } else {
      const {repository} = ctx.services;
      const removedUser = await repository.remove(Sector, ctx.request.body.username);
      if(!removedUser){
        ctx.status = 404;
      } else {
        ctx.body = {username: removedUser.username, status: true};
      }
    }
    await next();
  });

export {users};

@@ -251,16 +251,78 @@
 in'%5D);%0A%0A
+const authenticationAdmin = Authenticator(false, %5B'admin'%5D);%0A%0A
 const us
@@ -1108,43 +1108,178 @@
     
-const %7Brepository%7D = ctx.services;%0A
+if(ctx.request.body.tags)%7B%0A      ctx.status = 403;%0A    %7D else %7B%0A      const %7Brepository%7D = ctx.services;%0A      let user = ctx.request.body;%0A      user.tags = %5B'user'%5D;%0A  
     
@@ -1370,24 +1370,26 @@
 uest.body);%0A
+  
     if(!stor
@@ -1395,32 +1395,34 @@
 redUser)%7B%0A      
+  
 ctx.status = 403
@@ -1419,32 +1419,34 @@
 atus = 403;%0A    
+  
 %7D else %7B%0A      c
@@ -1430,32 +1430,34 @@
 %0A      %7D else %7B%0A
+  
       ctx.body =
@@ -1500,24 +1500,32 @@
 tus: true%7D;%0A
+      %7D%0A
     %7D%0A    aw
@@ -1591,32 +1591,606 @@
  (ctx, next)=%3E%7B%0A
+    if(ctx.request.body.tags)%7B%0A      ctx.status = 403;%0A    %7D else %7B%0A      if(ctx.request.body.username !== ctx.params.username)%7B%0A        ctx.status = 409;%0A      %7D else %7B%0A        const %7Brepository%7D = ctx.services;%0A        const updatedUser = await repository.update(Sector, ctx.request.body.username, ctx.request.body);%0A        if(!updatedUser)%7B%0A          ctx.status = 404;%0A        %7D else %7B%0A          ctx.body = %7Busername: updatedUser.username, status: true%7D;%0A        %7D%0A      %7D%0A    %7D%0A    await next();%0A  %7D)%0A  .put('/:username/tags', authenticationAdmin, async (ctx, next)=%3E%7B%0A
     if(ctx.reque
@@ -2560,34 +2560,16 @@
 %7D%0A    %7D%0A
-    await next();%0A
   %7D)%0A  .

disable changing of user tags