summaryrefslogtreecommitdiff
path: root/IkiWiki/UserInfo.pm
blob: dcf99da09bda07fbb5a643f3f4fa6a9d3a436f78 (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. # XXX deprecated, should be removed eventually
  63. sub get_banned_users () { #{{{
  64. my @ret;
  65. my $userinfo=userinfo_retrieve();
  66. foreach my $user (keys %{$userinfo}) {
  67. push @ret, $user if $userinfo->{$user}->{banned};
  68. }
  69. return @ret;
  70. } #}}}
  71. # XXX deprecated, should be removed eventually
  72. sub set_banned_users (@) { #{{{
  73. my %banned=map { $_ => 1 } @_;
  74. my $userinfo=userinfo_retrieve();
  75. foreach my $user (keys %{$userinfo}) {
  76. $userinfo->{$user}->{banned} = $banned{$user};
  77. }
  78. return userinfo_store($userinfo);
  79. } #}}}
  80. 1