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

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