tinycm view plugins/users/users.cgi @ rev 101

users plugin: speed up listing and improve ?lastusers page
author Christophe Lincoln <pankso@slitaz.org>
date Mon Feb 20 14:36:57 2017 +0100 (2017-02-20)
parents d6c0da38715f
children 69214a677729
line source
1 #!/bin/sh
2 #
3 # TinyCM/TazBug Plugin - Users profile and admin
4 #
6 # Display user public profile.
7 public_people() {
8 echo "</pre>"
9 }
11 # Display authenticated user profile. TODO: change password
12 auth_people() {
13 cat << EOT
14 Email : $MAIL
15 </pre>
17 <div id="tools">
18 $PLUGINS_TOOLS
19 <a href="$script?modprofile">$(gettext "Modify profile")</a>
20 </div>
21 EOT
22 }
24 # List last active users. Usage: last_users NB
25 list_last_users() {
26 count=${1}
27 echo "<h3>Last $count active users</h3>"
28 echo "<pre>"
29 find ${PEOPLE} -name "last" | xargs ls -1t | head -n ${count} | while read last;
30 do
31 dir="$(dirname $last)"
32 date="$(cat $last)"
33 u=$(basename $dir)
34 . "${PEOPLE}/${u}/account.conf"
35 cat << EOT
36 $(get_gravatar $MAIL 24) $date : <a href="?user=$u">$u</a> | $NAME
37 EOT
38 done
39 echo "</pre>"
40 }
42 case " $(GET) " in
43 *\ users\ *)
44 d="Users"
45 header
46 html_header
47 user_box
48 # Admin only
49 if admin_user; then
50 tools="<a href='$script?userslist'>Users list</a>"
51 fi
52 # Logged users
53 if check_auth; then
54 cat << EOT
55 <div id="tools">
56 <a href="$script?dashboard">Dashboard</a>
57 <a href='$script?lastusers'>Last users</a>
58 $tools
59 </div>
60 <h2>${d}</h2>
61 <pre>
62 User accounts : $(ls -1 $PEOPLE | wc -l)
63 Logged users : $(ls $sessions | wc -l)
64 </pre>
65 EOT
66 list_last_users 5
68 # Admin only
69 if admin_user; then
70 cat << EOT
71 <h3>Config paths</h3>
72 <pre>
73 People DB : $PEOPLE
74 Auth file : $AUTH_FILE
75 </pre>
76 EOT
77 # Get the list of administrators
78 echo "<h3>Admin users</h3>"
79 fgrep -l "ADMIN_USER=" $PEOPLE/*/account.conf | while read file;
80 do
81 . ${file}
82 echo "<a href='?user=$USER'>$USER</a>"
83 unset NAME USER
84 done
85 fi
87 else
88 gettext "You must be logged to check or admin users"
89 fi
90 html_footer && exit 0 ;;
92 *\ userslist\ *)
93 # List all users
94 d="Users"
95 header
96 html_header
97 user_box
98 if check_auth && ! admin_user; then
99 gettext "You must be admin to manage users"
100 exit 0
101 fi
102 users=$(ls -1 $PEOPLE | wc -l)
103 cat << EOT
104 <div id="tools">
105 <a href="$script?dashboard">Dashboard</a>
106 <a href="$script?users">Users</a>
107 <a href='$script?lastusers'>Last users</a>
108 </div>
109 <h2>Users: $users</h2>
110 <div id="users">
111 <table>
112 <thead>
113 <td>$(gettext "Username")</td>
114 <td>$(gettext "Action")</td>
115 </thead>
116 EOT
117 for u in $(ls $PEOPLE)
118 do
119 # Skip corrupted accounts
120 if ! [ -f "${PEOPLE}/${u}/account.conf" ]; then
121 echo "${u} : Missing account.conf"
122 continue
123 fi
124 cat << EOT
125 <tr>
126 <td><a href='?$u'>$u</a></td>
127 <td>TODO</td>
128 </tr>
129 EOT
130 # deluser link --> use 'tazu' on SliTaz
131 #: <a href="?users&amp;deluser=$USER">$(gettext "delete")</a>
132 unset NAME USER
133 done
134 echo "</table></div>"
135 html_footer && exit 0 ;;
137 *\ lastusers\ *)
138 # Show online users based on sessions files.
139 d="Last users"
140 header
141 html_header
142 user_box
143 if ! check_auth; then
144 gettext "You must be logged in to view online users"
145 exit 0
146 fi
147 cat << EOT
148 <div id="tools">
149 <a href="$script?dashboard">Dashboard</a>
150 <a href="$script?users">Users</a>
151 </div>
152 EOT
153 list_last_users 15
155 # Active cookies
156 echo "<h3>Session cookies: $(ls $sessions | wc -l)</h3>"
157 echo "<pre>"
158 for u in $(ls $sessions)
159 do
160 . "${PEOPLE}/${u}/account.conf"
161 cat << EOT
162 $(get_gravatar $MAIL 24) <a href="?user=$USER">$USER</a> | $NAME
163 EOT
164 done
165 echo "</pre>"
166 html_footer && exit 0 ;;
168 *\ user\ *)
169 # User profile page
170 d="$(GET user)"
171 last="$(cat $PEOPLE/"$(GET user)"/last)"
172 header
173 html_header
174 user_box
175 . $PEOPLE/"$(GET user)"/account.conf
176 cat << EOT
177 <h2>$(get_gravatar $MAIL) $NAME</h2>
179 <pre>
180 $(gettext "User name :") $USER
181 $(gettext "Last login :") $last
182 EOT
183 if check_auth && [ "$(GET user)" == "$user" ]; then
184 auth_people
185 else
186 # check_auth will set VARS to current logged user: re-source
187 . $PEOPLE/"$(GET user)"/account.conf
188 public_people
189 fi
191 # Messages plugin integration
192 if [ -x "$plugins/messages/messages.cgi" ]; then
193 if check_auth && [ "$(GET user)" != "$user" ]; then
194 cat << EOT
195 <div id="tools">
196 <a href="$script?messages&amp;to=$(GET user)">$(gettext "Send message")</a>
197 </div>
198 EOT
199 fi
200 fi
202 # Display personal user profile
203 if [ -f "$PEOPLE/$USER/profile.txt" ]; then
204 echo "<h2>$(gettext "About me")</h2>"
205 cat $PEOPLE/$USER/profile.txt | wiki_parser
206 fi
207 html_footer && exit 0 ;;
209 *\ modprofile\ *)
210 # Let user edit their profile
211 if ! check_auth; then
212 echo "ERROR" && exit 0
213 fi
214 d="$user profile"
215 path=${PEOPLE}/${user}
216 header
217 html_header
218 user_box
219 cat << EOT
220 <h2>$(gettext "User:") $user</h2>
221 <p>$(gettext "Modify your profile settings")
222 <div id="edit">
224 <form method="get" action="$script" name="editor">
225 <input type="hidden" name="saveprofile" />
226 <h3>Name</h3>
227 <input type="text" name="name" value="$NAME" />
228 <h3>Email</h3>
229 <input type="text" name="mail" value="$MAIL" />
230 <h3>About you</h3>
231 <textarea name="profile">$(cat "$path/profile.txt")</textarea>
232 <input type="submit" value="$(gettext "Save profile")" />
233 </form>
235 </div>
236 EOT
237 html_footer && exit 0 ;;
239 *\ saveprofile\ *)
240 # Save a user profile
241 if check_auth; then
242 path="$PEOPLE/$user"
243 sed -i s"/^NAME=.*/NAME=\"$(GET name)\"/" ${path}/account.conf
244 sed -i s"/^MAIL=.*/MAIL=\"$(GET mail)\"/" ${path}/account.conf
245 cp -f ${path}/profile.txt ${path}/profile.bak
246 sed "s/$(echo -en '\r') /\n/g" > ${path}/profile.txt << EOT
247 $(GET profile)
248 EOT
249 header "Location: $script?user=$user"
250 fi && exit 0 ;;
252 esac