[Broken] Revert changes to UserEncodePrivileges() and UserDecodePrivileges()

This commit is contained in:
Jordan Bancino 2023-11-08 09:29:23 -05:00
parent d60aac6d3a
commit 9b3b62ff30
5 changed files with 17 additions and 13 deletions

View file

@ -238,6 +238,7 @@ RegTokenCreate(Db * db, char *name, char *owner, UInt64 expires, Int64 uses, int
ret->uses = uses;
ret->created_on = timestamp;
ret->expires_on = expires;
/* TODO: #37 type mismatch: UserEncodePrivileges() returns a JsonValue */
ret->grants = UserEncodePrivileges(privileges);
return ret;

View file

@ -98,15 +98,15 @@ ROUTE_IMPL(RoutePrivileges, path, argp)
switch (HttpRequestMethodGet(args->context))
{
case HTTP_POST:
privileges = UserDecodePrivileges(JsonValueAsArray(val));
privileges = UserDecodePrivileges(val);
break;
case HTTP_PUT:
privileges = UserGetPrivileges(user);
privileges |= UserDecodePrivileges(JsonValueAsArray(val));
privileges |= UserDecodePrivileges(val);
break;
case HTTP_DELETE:
privileges = UserGetPrivileges(user);
privileges &= ~UserDecodePrivileges(JsonValueAsArray(val));
privileges &= ~UserDecodePrivileges(val);
break;
default:
/* Impossible */
@ -124,7 +124,7 @@ ROUTE_IMPL(RoutePrivileges, path, argp)
/* Fall through */
case HTTP_GET:
response = HashMapCreate();
HashMapSet(response, "privileges", JsonValueArray(UserEncodePrivileges(UserGetPrivileges(user))));
HashMapSet(response, "privileges", UserEncodePrivileges(UserGetPrivileges(user)));
break;
default:
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);

View file

@ -276,6 +276,7 @@ ROUTE_IMPL(RouteRegister, path, argp)
if (info)
{
/* TODO: #37 type mismatch: info->grants is a string array. */
UserSetPrivileges(user, UserDecodePrivileges(info->grants));
RegTokenClose(info);
RegTokenFree(info);

View file

@ -749,7 +749,7 @@ UserGetPrivileges(User * user)
return USER_NONE;
}
return UserDecodePrivileges(JsonValueAsArray(HashMapGet(DbJson(user->ref), "privileges")));
return UserDecodePrivileges(HashMapGet(DbJson(user->ref), "privileges"));
}
int
@ -768,7 +768,7 @@ UserSetPrivileges(User * user, int privileges)
return 1;
}
val = JsonValueArray(UserEncodePrivileges(privileges));
val = UserEncodePrivileges(privileges);
if (!val)
{
return 0;
@ -779,17 +779,19 @@ UserSetPrivileges(User * user, int privileges)
}
int
UserDecodePrivileges(Array * arr)
UserDecodePrivileges(JsonValue * val)
{
int privileges = USER_NONE;
Array *arr;
size_t i;
if (!arr)
if (!val || JsonValueType(val) != JSON_ARRAY)
{
goto finish;
}
arr = JsonValueAsArray(val);
for (i = 0; i < ArraySize(arr); i++)
{
JsonValue *val = ArrayGet(arr, i);
@ -846,7 +848,7 @@ UserDecodePrivilege(const char *p)
}
}
Array *
JsonValue *
UserEncodePrivileges(int privileges)
{
Array *arr = ArrayCreate();
@ -878,7 +880,7 @@ UserEncodePrivileges(int privileges)
#undef A
finish:
return arr;
return JsonValueArray(arr);
}
UserId *

View file

@ -286,13 +286,13 @@ extern int UserSetPrivileges(User *, int);
* Decode the JSON that represents the user privileges into a packed
* bit field for simple manipulation.
*/
extern int UserDecodePrivileges(Array *);
extern int UserDecodePrivileges(JsonValue *);
/**
* Encode the packed bit field that represents user privileges as a
* JSON value.
*/
extern Array *UserEncodePrivileges(int);
extern JsonValue *UserEncodePrivileges(int);
/**
* Convert a string privilege into its bit in the bit field. This is