关键词搜索

源码搜索 ×
×

漫话Redis源码之一百一十九

发布2022-06-19浏览372次

详情内容

这个注释很清楚了:

/* Create a new user with the specified name, store it in the list
 * of users (the Users global radix tree), and returns a reference to
 * the structure representing the user.
 *
 * If the user with such name already exists NULL is returned. */

  1. user *ACLCreateUser(const char *name, size_t namelen) {
  2. if (raxFind(Users,(unsigned char*)name,namelen) != raxNotFound) return NULL;
  3. user *u = zmalloc(sizeof(*u));
  4. u->name = sdsnewlen(name,namelen);
  5. u->flags = USER_FLAG_DISABLED | server.acl_pubsub_default;
  6. u->allowed_subcommands = NULL;
  7. u->passwords = listCreate();
  8. u->patterns = listCreate();
  9. u->channels = listCreate();
  10. listSetMatchMethod(u->passwords,ACLListMatchSds);
  11. listSetFreeMethod(u->passwords,ACLListFreeSds);
  12. listSetDupMethod(u->passwords,ACLListDupSds);
  13. listSetMatchMethod(u->patterns,ACLListMatchSds);
  14. listSetFreeMethod(u->patterns,ACLListFreeSds);
  15. listSetDupMethod(u->patterns,ACLListDupSds);
  16. listSetMatchMethod(u->channels,ACLListMatchSds);
  17. listSetFreeMethod(u->channels,ACLListFreeSds);
  18. listSetDupMethod(u->channels,ACLListDupSds);
  19. memset(u->allowed_commands,0,sizeof(u->allowed_commands));
  20. raxInsert(Users,(unsigned char*)name,namelen,u,NULL);
  21. return u;
  22. }
  23. /* This function should be called when we need an unlinked "fake" user
  24. * we can use in order to validate ACL rules or for other similar reasons.
  25. * The user will not get linked to the Users radix tree. The returned
  26. * user should be released with ACLFreeUser() as usually. */
  27. user *ACLCreateUnlinkedUser(void) {
  28. char username[64];
  29. for (int j = 0; ; j++) {
  30. snprintf(username,sizeof(username),"__fakeuser:%d__",j);
  31. user *fakeuser = ACLCreateUser(username,strlen(username));
  32. if (fakeuser == NULL) continue;
  33. int retval = raxRemove(Users,(unsigned char*) username,
  34. strlen(username),NULL);
  35. serverAssert(retval != 0);
  36. return fakeuser;
  37. }
  38. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载