summaryrefslogtreecommitdiff
path: root/IkiWiki/UserInfo.pm
blob: a0b1327b44fdeac0b3b37e46e863de95a0ad360e (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Storable;
  5. use IkiWiki;
  6. package 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. sub get_banned_users () { #{{{
  63. my @ret;
  64. my $userinfo=userinfo_retrieve();
  65. foreach my $user (keys %{$userinfo}) {
  66. push @ret, $user if $userinfo->{$user}->{banned};
  67. }
  68. return @ret;
  69. } #}}}
  70. sub set_banned_users (@) { #{{{
  71. my %banned=map { $_ => 1 } @_;
  72. my $userinfo=userinfo_retrieve();
  73. foreach my $user (keys %{$userinfo}) {
  74. $userinfo->{$user}->{banned} = $banned{$user};
  75. }
  76. return userinfo_store($userinfo);
  77. } #}}}
  78. 1