/[LeafOK_CVS]/lbbs/src/user_priv.c
ViewVC logotype

Contents of /lbbs/src/user_priv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Mon Apr 28 12:45:57 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.6: +34 -57 lines
Content type: text/x-csrc
Remove legacy code and refine

1 /***************************************************************************
2 user_priv.c - description
3 -------------------
4 begin : Mon Oct 22 2004
5 copyright : (C) 2004 by Leaflet
6 email : leaflet@leafok.com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "bbs.h"
19 #include "common.h"
20 #include <mysql.h>
21
22 int checklevel(BBS_user_priv *p_priv, int level)
23 {
24 return (((p_priv->level & level)) ^ level ? 0 : 1);
25 }
26
27 int setpriv(BBS_user_priv *p_priv, int sid, int priv)
28 {
29 int i;
30 if (sid > 0)
31 {
32 for (i = 0; i < p_priv->s_count; i++)
33 {
34 if (p_priv->s_priv_list[i].sid == sid)
35 {
36 p_priv->s_priv_list[i].s_priv = priv;
37 return 0;
38 }
39 }
40 if (i < BBS_max_section)
41 {
42 p_priv->s_priv_list[i].s_priv = priv;
43 }
44 else
45 {
46 return -1;
47 }
48 }
49 else
50 {
51 p_priv->g_priv = priv;
52 }
53
54 return 0;
55 }
56
57 int getpriv(BBS_user_priv *p_priv, int sid)
58 {
59 int i;
60 for (i = 0; i < p_priv->s_count; i++)
61 {
62 if (p_priv->s_priv_list[i].sid == sid)
63 return p_priv->s_priv_list[i].s_priv;
64 }
65
66 return (sid >= 0 ? p_priv->g_priv : S_NONE);
67 }
68
69 int checkpriv(BBS_user_priv *p_priv, int sid, int priv)
70 {
71 return (((getpriv(p_priv, sid) & priv)) ^ priv ? 0 : 1);
72 }
73
74 int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)
75 {
76 MYSQL_RES *rs;
77 MYSQL_ROW row;
78 char sql[1024];
79 int i;
80
81 p_priv->uid = uid;
82 p_priv->level = (uid == 0 ? P_GUEST : P_USER);
83 p_priv->g_priv = S_DEFAULT;
84
85 if (db == NULL)
86 return 1;
87
88 // Permission
89 sprintf(sql, "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",
90 uid);
91 if (mysql_query(db, sql) != 0)
92 {
93 log_error("Query user_list failed\n");
94 return -1;
95 }
96 if ((rs = mysql_store_result(db)) == NULL)
97 {
98 log_error("Get user_list data failed\n");
99 return -1;
100 }
101 if (row = mysql_fetch_row(rs))
102 {
103 p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
104 p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
105 }
106 mysql_free_result(rs);
107
108 // Admin
109 sprintf(sql, "SELECT major FROM admin_config WHERE UID = %ld "
110 "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
111 uid);
112 if (mysql_query(db, sql) != 0)
113 {
114 log_error("Query admin_config failed\n");
115 return -1;
116 }
117 if ((rs = mysql_store_result(db)) == NULL)
118 {
119 log_error("Get admin_config data failed\n");
120 return -1;
121 }
122 if (row = mysql_fetch_row(rs))
123 {
124 p_priv->level |= (atoi(row[1]) ? P_ADMIN_M : P_ADMIN_S);
125 p_priv->g_priv |= (atoi(row[1]) ? S_ALL : S_ADMIN);
126 }
127 mysql_free_result(rs);
128
129 // Section Master
130 sprintf(sql, "SELECT section_master.SID, major FROM section_master "
131 "INNER JOIN section_config ON section_master.SID = section_config.SID "
132 "WHERE UID = %ld AND section_master.enable AND section_config.enable "
133 "AND (NOW() BETWEEN begin_dt AND end_dt)",
134 uid);
135 if (mysql_query(db, sql) != 0)
136 {
137 log_error("Query section_master failed\n");
138 return -1;
139 }
140 if ((rs = mysql_store_result(db)) == NULL)
141 {
142 log_error("Get section_master data failed\n");
143 return -1;
144 }
145 while (row = mysql_fetch_row(rs))
146 {
147 p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
148 setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
149 }
150 mysql_free_result(rs);
151
152 // Section status
153 sprintf(sql, "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
154 "INNER JOIN section_class ON section_config.CID = section_class.CID "
155 "WHERE section_config.enable AND section_class.enable "
156 "ORDER BY SID");
157 if (mysql_query(db, sql) != 0)
158 {
159 log_error("Query section_config failed\n");
160 return -1;
161 }
162 if ((rs = mysql_store_result(db)) == NULL)
163 {
164 log_error("Get section_config data failed\n");
165 return -1;
166 }
167 while (row = mysql_fetch_row(rs))
168 {
169 int priv = getpriv(p_priv, atoi(row[0]));
170 if (p_priv->level < atoi(row[2]))
171 {
172 priv &= (~S_LIST);
173 }
174 if (p_priv->level < atoi(row[3]))
175 {
176 priv &= (~S_POST);
177 }
178 if (!atoi(row[1]))
179 {
180 priv &= (~S_GETEXP);
181 }
182 setpriv(p_priv, atoi(row[0]), priv);
183 }
184 mysql_free_result(rs);
185
186 // Section ban
187 sprintf(sql, "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
188 "AND (NOW() BETWEEN ban_dt AND unban_dt)",
189 uid);
190 if (mysql_query(db, sql) != 0)
191 {
192 log_error("Query ban_user_list failed\n");
193 return -1;
194 }
195 if ((rs = mysql_store_result(db)) == NULL)
196 {
197 log_error("Get ban_user_list data failed\n");
198 return -1;
199 }
200 while (row = mysql_fetch_row(rs))
201 {
202 setpriv(p_priv, atoi(row[0]),
203 getpriv(p_priv, atoi(row[0])) & (~S_POST));
204 }
205 mysql_free_result(rs);
206
207 return 0;
208 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1