summaryrefslogtreecommitdiff
path: root/IkiWiki/UserInfo.pm
blob: 0bf100a959c934d23c8f73ac1f0962f856732a35 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Storable;
  6. use IkiWiki;
  7. sub userinfo_retrieve () {
  8. my $userinfo=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  9. return $userinfo;
  10. }
  11. sub userinfo_store ($) {
  12. my $userinfo=shift;
  13. my $newfile="$config{wikistatedir}/userdb.new";
  14. my $oldmask=umask(077);
  15. my $ret=Storable::lock_nstore($userinfo, $newfile);
  16. umask($oldmask);
  17. if (defined $ret && $ret) {
  18. if (! rename($newfile, "$config{wikistatedir}/userdb")) {
  19. unlink($newfile);
  20. $ret=undef;
  21. }
  22. }
  23. return $ret;
  24. }
  25. sub userinfo_get ($$) {
  26. my $user=shift;
  27. my $field=shift;
  28. my $userinfo=userinfo_retrieve();
  29. if (! defined $userinfo ||
  30. ! exists $userinfo->{$user} || ! ref $userinfo->{$user} ||
  31. ! exists $userinfo->{$user}->{$field}) {
  32. return "";
  33. }
  34. return $userinfo->{$user}->{$field};
  35. }
  36. sub userinfo_set ($$$) {
  37. my $user=shift;
  38. my $field=shift;
  39. my $value=shift;
  40. my $userinfo=userinfo_retrieve();
  41. if (! defined $userinfo ||
  42. ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
  43. return "";
  44. }
  45. $userinfo->{$user}->{$field}=$value;
  46. return userinfo_store($userinfo);
  47. }
  48. sub userinfo_setall ($$) {
  49. my $user=shift;
  50. my $info=shift;
  51. my $userinfo=userinfo_retrieve();
  52. if (! defined $userinfo) {
  53. $userinfo={};
  54. }
  55. $userinfo->{$user}=$info;
  56. return userinfo_store($userinfo);
  57. }
  58. sub is_admin ($) {
  59. my $user_name=shift;
  60. return grep { $_ eq $user_name } @{$config{adminuser}};
  61. }
  62. 1