summaryrefslogtreecommitdiff
path: root/htroot/ConfigAccountList_p.java
blob: e6ee44f634b757e475911ca6dc5730788504c914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// ConfigAccountList_p.java
// -------------------------
// (c) 2017 by reger24; https://github.com/reger24
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import java.util.Date;
import java.util.Iterator;
import net.yacy.cora.date.GenericFormatter;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.data.UserDB;
import net.yacy.data.UserDB.AccessRight;
import net.yacy.search.Switchboard;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;

public class ConfigAccountList_p {

    public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, final serverObjects post, final serverSwitch env) {

        final serverObjects prop = new serverObjects();
        final Switchboard sb = (Switchboard) env;
        UserDB.Entry entry;

        if (sb.userDB == null) {
            prop.put("userlist", 0);
            return prop;
        }

        //Generate Userlist
        final Iterator<UserDB.Entry> it = sb.userDB.iterator(true);
        int numUsers = 0;
        while (it.hasNext()) {
            entry = it.next();
            if (entry == null) {
                continue;
            }
            prop.putHTML("userlist_" + numUsers + "_username", entry.getUserName());
            prop.putHTML("userlist_" + numUsers + "_lastname", entry.getLastName());
            prop.putHTML("userlist_" + numUsers + "_firstname", entry.getFirstName());
            prop.putHTML("userlist_" + numUsers + "_address", entry.getAddress());
            if (entry.getLastAccess() != null) {
                prop.put("userlist_" + numUsers + "_lastaccess", GenericFormatter.FORMAT_SIMPLE.format(new Date(entry.getLastAccess())));
            } else {
                prop.put("userlist_" + numUsers + "_lastaccess", "never");
            }

            final AccessRight[] rights = AccessRight.values();
            String rightStr = "";
            for (final AccessRight right : rights) {
                if (entry.hasRight(right)) {
                    if (rightStr.isEmpty()) {
                        rightStr = right.getFriendlyName();
                    } else {
                        rightStr += ", " + right.getFriendlyName();
                    }
                }
            }
            prop.putHTML("userlist_" + numUsers + "_rights", rightStr);

            long percent;
            if (entry.getTrafficLimit() != null) {
                long limit = entry.getTrafficLimit();
                long used = entry.getTrafficSize();
                percent = used * 100 / limit;
                prop.put("userlist_" + numUsers + "_time", percent);
            } else {
                prop.put("userlist_" + numUsers + "_time", "");
            }

            percent = -1;
            if (entry.getTimeLimit() > 0) {
                long limit = entry.getTimeLimit();
                long used = entry.getTimeUsed();
                percent = used * 100 / limit;
                prop.put("userlist_" + numUsers + "_traffic", percent);
            } else {
                prop.put("userlist_" + numUsers + "_traffic", "");
            }


            numUsers++;
        }
        prop.put("userlist", numUsers);

        // return rewrite properties
        return prop;
    }
}