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

Fix admin_user
author Christophe Lincoln <pankso@slitaz.org>
date Wed Feb 22 12:22:11 2017 +0100 (2017-02-22)
parents ddf5125a4b03
children 5acf8ea65f7d
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 Authfile : $AUTH_FILE
75 Admin users : $ADMIN_USERS
76 </pre>
77 EOT
78 # Get the list of administrators
79 echo "<h3>Admin users</h3>"
80 echo "<pre>"
81 for u in $(cat $ADMIN_USERS)
82 do
83 . ${PEOPLE}/${u}/account.conf
84 echo "<a href='?user=$u'>$u</a> - $NAME"
85 done
86 echo "</pre>"
87 fi
89 else
90 gettext "You must be logged in to check on admin users"
91 fi
92 html_footer && exit 0 ;;
94 *\ userslist\ *)
95 # List all users
96 d="Users"
97 header
98 html_header
99 user_box
100 if check_auth && ! admin_user; then
101 gettext "You must be admin to manage users"
102 exit 0
103 fi
104 users=$(ls -1 $PEOPLE | wc -l)
105 cat << EOT
106 <div id="tools">
107 <a href="$script?dashboard">Dashboard</a>
108 <a href="$script?users">Users</a>
109 <a href='$script?lastusers'>Last users</a>
110 </div>
111 <h2>Users: $users</h2>
112 <div id="users">
113 <table>
114 <thead>
115 <td>$(gettext "Username")</td>
116 <td>$(gettext "Action")</td>
117 </thead>
118 EOT
119 for u in $(ls $PEOPLE)
120 do
121 # Skip corrupted accounts
122 if ! [ -f "${PEOPLE}/${u}/account.conf" ]; then
123 echo "${u} : Missing account.conf"
124 continue
125 fi
126 cat << EOT
127 <tr>
128 <td><a href="$script?user=$u">$u</a></td>
129 <td>TODO</td>
130 </tr>
131 EOT
132 # deluser link --> use 'tazu' on SliTaz
133 #: <a href="?users&amp;deluser=$USER">$(gettext "delete")</a>
134 unset NAME USER
135 done
136 echo "</table></div>"
137 html_footer && exit 0 ;;
139 *\ lastusers\ *)
140 # Show online users based on sessions files.
141 d="Last users"
142 header
143 html_header
144 user_box
145 if ! check_auth; then
146 gettext "You must be logged in to view online users"
147 exit 0
148 fi
149 cat << EOT
150 <div id="tools">
151 <a href="$script?dashboard">Dashboard</a>
152 <a href="$script?users">Users</a>
153 </div>
154 EOT
155 list_last_users 15
157 # Active cookies
158 echo "<h3>Session cookies: $(ls $sessions | wc -l)</h3>"
159 echo "<pre>"
160 for u in $(ls $sessions)
161 do
162 . "${PEOPLE}/${u}/account.conf"
163 cat << EOT
164 $(get_gravatar $MAIL 24) <a href="?user=$USER">$USER</a> | $NAME
165 EOT
166 done
167 echo "</pre>"
168 html_footer && exit 0 ;;
170 *\ user\ *)
171 # User profile page
172 d="$(GET user)"
173 last="$(cat $PEOPLE/"$(GET user)"/last)"
174 header
175 html_header
176 user_box
177 account_config="$PEOPLE/$(GET user)/account.conf"
178 profile_config="$PEOPLE/$(GET user)/profile.conf"
180 if [ ! -f "$account_config" ]; then
181 echo "No user profile for: $(GET user)"
182 html_footer && exit 0
183 else
184 . ${account_config}
185 fi
186 # Init profile.conf
187 if [ -f "${profile_config}" ]; then
188 . ${profile_config}
189 else
190 cat > $PEOPLE/$user/profile.conf << EOT
191 # User profile
192 WEBSITE="$website"
193 FACEBOOK="$facebook"
194 TWITTER="$twitter"
195 EOT
196 chmod 0600 $PEOPLE/$user/profile.conf
197 fi
198 cat << EOT
199 <h2>$(get_gravatar $MAIL) $NAME</h2>
201 <pre>
202 $(gettext "User name :") $USER
203 $(gettext "Last login :") $last
204 EOT
205 if check_auth && [ "$(GET user)" == "$user" ]; then
206 auth_people
207 else
208 # check_auth will set VARS to current logged user: re-source
209 . $PEOPLE/"$(GET user)"/account.conf
210 public_people
211 fi
213 # Messages plugin integration
214 if [ -x "$plugins/messages/messages.cgi" ]; then
215 if check_auth && [ "$(GET user)" != "$user" ]; then
216 cat << EOT
217 <div id="tools">
218 <a href="$script?messages&amp;to=$(GET user)">$(gettext "Send message")</a>
219 </div>
220 EOT
221 fi
222 fi
224 # Display personal user profile
225 if [ -f "$PEOPLE/$USER/profile.txt" ]; then
226 echo "<h2>$(gettext "About me")</h2>"
227 cat $PEOPLE/$USER/profile.txt | wiki_parser
228 fi
229 html_footer && exit 0 ;;
231 *\ modprofile\ *)
232 # Let user edit their profile
233 if ! check_auth; then
234 echo "ERROR" && exit 0
235 fi
236 d="$user profile"
237 path=${PEOPLE}/${user}
238 header
239 html_header
240 user_box
241 cat << EOT
242 <h2>$(gettext "User:") $user</h2>
243 <p>$(gettext "Modify your profile settings")
244 <div id="edit">
246 <form method="get" action="$script" name="editor">
247 <input type="hidden" name="saveprofile" />
248 <h3>Name</h3>
249 <input type="text" name="name" value="$NAME" />
250 <h3>Email</h3>
251 <input type="text" name="mail" value="$MAIL" />
252 <h3>About you</h3>
253 <textarea name="profile">$(cat "$path/profile.txt")</textarea>
254 <input type="submit" value="$(gettext "Save profile")" />
255 </form>
257 </div>
258 EOT
259 html_footer && exit 0 ;;
261 *\ saveprofile\ *)
262 # Save a user profile
263 if check_auth; then
264 path="$PEOPLE/$user"
265 sed -i s"/^NAME=.*/NAME=\"$(GET name)\"/" ${path}/account.conf
266 sed -i s"/^MAIL=.*/MAIL=\"$(GET mail)\"/" ${path}/account.conf
267 cp -f ${path}/profile.txt ${path}/profile.bak
268 sed "s/$(echo -en '\r') /\n/g" > ${path}/profile.txt << EOT
269 $(GET profile)
270 EOT
271 header "Location: $script?user=$user"
272 fi && exit 0 ;;
274 esac