KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdRightChange


1 /**
2  * Copyright (C) 2003 Jochen
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 31.08.2004
19  */

20 package freecs.commands;
21
22 import freecs.content.MessageState;
23 import freecs.core.User;
24 import freecs.core.UserManager;
25 import freecs.interfaces.ICommand;
26 import freecs.interfaces.IUserStates;
27
28 /**
29  * @author Jochen Schurich
30  *
31  * a user can assign/revoke rights to other user(s) <br>
32  *
33  *
34  */

35 public class CmdRightChange extends AbstractCommand {
36     public final String JavaDoc cmd = "/rc";
37     private static final ICommand self = new CmdRightChange();
38
39     private CmdRightChange() { }
40
41     public static ICommand getInstance() {
42         return self;
43     }
44
45     public boolean execute(MessageState msgState, String JavaDoc param) {
46         if (param.length() < 1) {
47             msgState.msgTemplate = "error.rc.noArg";
48             msgState.sender.sendMessage(msgState.mp);
49             return false;
50         }
51         String JavaDoc[] parts = param.split(" ");
52         param = parts[0];
53         // look for the user using utility-method of AbsractCommand
54
User u = getUser(msgState, param);
55         if (u == null)
56             return false;
57         msgState.usercontext = u;
58
59         // no rights to assign or revoke found
60
if (parts.length < 2) {
61             // reset user to default rights
62
if(u.compareRoleTo(msgState.sender) == 2) {
63                  msgState.msgTemplate="error.noRight.noVipAdmin";
64                  msgState.sender.sendMessage(msgState.mp);
65                  return false;
66             } else if(u.compareRoleTo(msgState.sender) == 3) {
67                        msgState.msgTemplate="error.noRight.noAdmin";
68                        msgState.sender.sendMessage(msgState.mp);
69                        return false;
70             } else if (u.compareRoleTo(msgState.sender) >= 0) {
71                          if (u.getPermissionMap() == u.getDefaultPermissionMap())
72                              return false ;
73                          u.resetPermission();
74                          msgState.msgTemplate = "rc.rightsReset";
75                          msgState.sender.sendMessage(msgState.mp);
76                          msgState.msgTemplate = "rc.newResetRight";
77                          u.sendMessage(msgState.mp);
78             } else {
79                 msgState.param = "reset";
80                 msgState.msgTemplate = "error.rc.notRight";
81                 msgState.sender.sendMessage(msgState.mp);
82             }
83
84             return true;
85         }
86
87         // all new rights have to be lower than the "donating" user and higher
88
// than the "accepting" user
89
for (int i = 1; i < parts.length; i++) {
90             String JavaDoc part = parts[i];
91             if (part.length() < 2)
92                 continue;
93             if (part.startsWith("+")) {
94                 // add right
95
part = part.substring(1);
96                 msgState.param = part;
97                 int right = UserManager.resolveState(part);
98                 if (right != 0) {
99                     if (right== IUserStates.IS_GUEST || right== IUserStates.IS_MODERATOR) {
100                         if (allowedToChangeRight(right,msgState.sender)) {
101                             u.givePermission(right);
102                             msgState.msgTemplate = "rc.rightAssigned";
103                             msgState.sender.sendMessage(msgState.mp);
104                             msgState.msgTemplate = "rc.newAssignedRight";
105                             u.sendMessage(msgState.mp);
106                             continue;
107                         } else {
108                             msgState.msgTemplate = "error.rc.notRight";
109                             msgState.sender.sendMessage(msgState.mp);
110                             continue;
111                         }
112                     }
113                 }
114             } else if (part.startsWith("-")) {
115                 // remove right
116
part = part.substring(1);
117                 msgState.param = part;
118                 int right = UserManager.resolveState(part);
119                 if (right != 0) {
120                     if (right== IUserStates.IS_GUEST || right== IUserStates.IS_MODERATOR) {
121                         if (allowedToChangeRight(right,msgState.sender)) {
122                             u.takePermission(right);
123                             msgState.msgTemplate = "rc.rightRevoked";
124                             msgState.sender.sendMessage(msgState.mp);
125                             msgState.msgTemplate = "rc.newRevokedRight";
126                             u.sendMessage(msgState.mp);
127                             continue;
128                         } else {
129                             msgState.msgTemplate = "error.noRight.noMOD";
130                             msgState.sender.sendMessage(msgState.mp);
131                             continue;
132                         }
133                     }
134                 }
135             }
136             if (part.startsWith("=")) {
137                 part = part.substring(1);
138                 msgState.param = part;
139                 int right = UserManager.resolveState(part);
140                 if (right != 0) {
141                     if (right== IUserStates.ROLE_USER || right== IUserStates.ROLE_VIP || right== IUserStates.ROLE_ASSHOLE ) {
142                         if (allowedToChangeRight(right,msgState.sender)) {
143                             u.setNewPermission(right);
144                             msgState.msgTemplate = "rc.rightAssigned";
145                             msgState.sender.sendMessage(msgState.mp);
146                             msgState.msgTemplate = "rc.newAssignedRight";
147                             u.sendMessage(msgState.mp);
148                             continue;
149                         } else {
150                             msgState.msgTemplate = "error.noRight.noAdmin";
151                             msgState.sender.sendMessage(msgState.mp);
152                             continue;
153                         }
154                     }
155                 }
156             }
157             // ignore parameter
158
msgState.msgTemplate = "error.rc.rightNotFound";
159             msgState.sender.sendMessage(msgState.mp);
160         }
161
162         return true;
163     }
164
165     public static boolean allowedToChangeRight(int newRight, User donatingUser) {
166         if (donatingUser.hasRole(IUserStates.ROLE_GOD)) {
167             // GOD can do EVERYTHING!
168
return true;
169         } else if (donatingUser.hasRight(IUserStates.IS_MODERATOR)) {
170             // MODERATOR's may assign Guest's
171
if (newRight == IUserStates.IS_GUEST)
172                 return true;
173             if (newRight == IUserStates.IS_MODERATOR)
174                 return true;
175         } /* else if (donatingUser.hasRole(IUserStates.ROLE_VIP)) {
176             // VIP's may assign MODERATOR's and GUEST's
177             if (newRight == IUserStates.IS_GUEST)
178                 return true;
179             if (newRight == IUserStates.IS_MODERATOR)
180                 return true;
181         } */

182         return false;
183     }
184
185 }
Popular Tags