1 package Newslash::Plugin::ResponseFilter;
2 use Mojo::Base 'Mojolicious::Plugin';
6 my ($self, $app, $conf) = @_;
8 # set default config value
9 my $cnf = $app->config->{ResponseFilter} ||= {};
10 my $default_faculities = { users => { 10000 => [qw(realemail passwd newpasswd newpasswd_ts)] },
11 comments => { 1000 => [qw(ipid subnetid)] },
12 metamoderations => { 1000 => [qw(uid)] },
13 moderations => { 1000 => [qw(ipid subnetid uid m2uid)] },
14 stories => { 1000 => [qw(hits hitparade)] },
15 submissions => { 1000 => [qw(ipid subnetid note comment)] },
17 $cnf->{faculities} ||= $default_faculities;
19 $app->helper(apply_seclev_filter => sub { apply_seclev_filter(@_) });
22 $app->hook(before_render => sub {
24 return if (!defined $args->{json});
26 my $json = $args->{json};
28 if (defined $json->{result} && ref($json->{result}) eq "HASH") {
29 $result = $json->{result};
32 if ($result->{item}) {
33 my $type = $result->{item}->{content_type};
35 apply_seclev_filter($c, $type, $result->{item});
39 if ($result->{items} && ref($result->{items}) eq "ARRAY") {
40 for my $item (@{$result->{items}}) {
41 my $type = $item->{content_type};
43 apply_seclev_filter($c, $type, $item);
51 sub apply_seclev_filter {
52 my ($c, $model, $items) = @_;
53 if (!$items || !$model) {
54 $c->app->log->error("ResponseFilter: model or items not given");
58 my $user = $c->stash('user') || {};
59 my $security_level = $user->{seclev} || 0;
62 if (ref($items) ne "ARRAY") {
66 my $cnf = $c->config->{ResponseFilter} || {};
67 my $faculties = $cnf->{faculities} || {};
69 my $faculty_all = $faculties->{all} || {};
70 for my $lv (keys %$faculty_all) {
71 if ($security_level < $lv) {
72 for my $item (@$targets) {
73 for my $k (@{$faculty_all->{$lv}}) {
74 if (defined $item->{$k}) {
76 #$c->app->log->debug("remove $k");
83 my $faculty = $faculties->{$model} || {};
84 for my $lv (keys %$faculty) {
85 if ($security_level < $lv) {
86 for my $item (@$targets) {
87 for my $k (@{$faculty->{$lv}}) {
88 if (defined $item->{$k}) {
90 #$c->app->log->debug("remove $k");