2657 |
22 Sep 06 |
martin |
1 |
/* |
2657 |
22 Sep 06 |
martin |
$Id$ |
2657 |
22 Sep 06 |
martin |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg, Martin Svensson |
2657 |
22 Sep 06 |
martin |
5 |
|
2657 |
22 Sep 06 |
martin |
This file is part of BASE - BioArray Software Environment. |
2657 |
22 Sep 06 |
martin |
Available at http://base.thep.lu.se/ |
2657 |
22 Sep 06 |
martin |
8 |
|
2657 |
22 Sep 06 |
martin |
BASE is free software; you can redistribute it and/or |
2657 |
22 Sep 06 |
martin |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2657 |
22 Sep 06 |
martin |
of the License, or (at your option) any later version. |
2657 |
22 Sep 06 |
martin |
13 |
|
2657 |
22 Sep 06 |
martin |
BASE is distributed in the hope that it will be useful, |
2657 |
22 Sep 06 |
martin |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2657 |
22 Sep 06 |
martin |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2657 |
22 Sep 06 |
martin |
GNU General Public License for more details. |
2657 |
22 Sep 06 |
martin |
18 |
|
2657 |
22 Sep 06 |
martin |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2657 |
22 Sep 06 |
martin |
21 |
*/ |
2657 |
22 Sep 06 |
martin |
22 |
|
2657 |
22 Sep 06 |
martin |
23 |
import net.sf.basedb.core.BaseException; |
2657 |
22 Sep 06 |
martin |
24 |
import net.sf.basedb.core.DbControl; |
2722 |
11 Oct 06 |
nicklas |
25 |
import net.sf.basedb.core.InvalidDataException; |
2722 |
11 Oct 06 |
nicklas |
26 |
import net.sf.basedb.core.News; |
2657 |
22 Sep 06 |
martin |
27 |
import net.sf.basedb.core.PermissionDeniedException; |
2657 |
22 Sep 06 |
martin |
28 |
import net.sf.basedb.core.ProgressReporter; |
2657 |
22 Sep 06 |
martin |
29 |
import net.sf.basedb.core.plugin.AbstractPlugin; |
2657 |
22 Sep 06 |
martin |
30 |
import net.sf.basedb.core.plugin.Plugin; |
2657 |
22 Sep 06 |
martin |
31 |
import net.sf.basedb.core.plugin.Request; |
2657 |
22 Sep 06 |
martin |
32 |
import net.sf.basedb.core.plugin.Response; |
2657 |
22 Sep 06 |
martin |
33 |
|
2722 |
11 Oct 06 |
nicklas |
34 |
import java.util.ArrayList; |
2657 |
22 Sep 06 |
martin |
35 |
import java.util.Arrays; |
2722 |
11 Oct 06 |
nicklas |
36 |
import java.util.Date; |
2722 |
11 Oct 06 |
nicklas |
37 |
import java.util.List; |
2657 |
22 Sep 06 |
martin |
38 |
|
2657 |
22 Sep 06 |
martin |
39 |
public class PermissionPlugin |
2657 |
22 Sep 06 |
martin |
40 |
extends AbstractPlugin |
2657 |
22 Sep 06 |
martin |
41 |
{ |
2657 |
22 Sep 06 |
martin |
42 |
|
6127 |
14 Sep 12 |
nicklas |
43 |
@Override |
2657 |
22 Sep 06 |
martin |
44 |
public MainType getMainType() |
2657 |
22 Sep 06 |
martin |
45 |
{ |
2657 |
22 Sep 06 |
martin |
46 |
return Plugin.MainType.OTHER; |
2657 |
22 Sep 06 |
martin |
47 |
} |
6127 |
14 Sep 12 |
nicklas |
48 |
@Override |
2657 |
22 Sep 06 |
martin |
49 |
public boolean supportsConfigurations() |
2657 |
22 Sep 06 |
martin |
50 |
{ |
2657 |
22 Sep 06 |
martin |
51 |
return false; |
2657 |
22 Sep 06 |
martin |
52 |
} |
2657 |
22 Sep 06 |
martin |
53 |
|
6127 |
14 Sep 12 |
nicklas |
54 |
@Override |
2657 |
22 Sep 06 |
martin |
55 |
public boolean requiresConfiguration() |
2657 |
22 Sep 06 |
martin |
56 |
{ |
2657 |
22 Sep 06 |
martin |
57 |
return false; |
2657 |
22 Sep 06 |
martin |
58 |
} |
2657 |
22 Sep 06 |
martin |
59 |
|
6127 |
14 Sep 12 |
nicklas |
60 |
@Override |
2722 |
11 Oct 06 |
nicklas |
61 |
public void run(Request request, Response response, ProgressReporter progress) |
2657 |
22 Sep 06 |
martin |
62 |
throws BaseException |
2657 |
22 Sep 06 |
martin |
63 |
{ |
2722 |
11 Oct 06 |
nicklas |
64 |
String cmd = (String)job.getValue("cmd"); |
2722 |
11 Oct 06 |
nicklas |
65 |
boolean shouldFail = (Boolean)job.getValue("shouldFail"); |
2722 |
11 Oct 06 |
nicklas |
66 |
|
2657 |
22 Sep 06 |
martin |
67 |
try |
2722 |
11 Oct 06 |
nicklas |
68 |
{ |
2722 |
11 Oct 06 |
nicklas |
69 |
if ("create".equals(cmd)) |
2722 |
11 Oct 06 |
nicklas |
70 |
{ |
2722 |
11 Oct 06 |
nicklas |
71 |
try |
2722 |
11 Oct 06 |
nicklas |
72 |
{ |
2722 |
11 Oct 06 |
nicklas |
73 |
createNews(shouldFail ? |
2722 |
11 Oct 06 |
nicklas |
74 |
"This news should not be here because we expected a PermissionDeniedException" : |
2722 |
11 Oct 06 |
nicklas |
75 |
"This news should be here since we now have CREATE permission"); |
2722 |
11 Oct 06 |
nicklas |
76 |
} |
2722 |
11 Oct 06 |
nicklas |
77 |
catch (PermissionDeniedException ex) |
2722 |
11 Oct 06 |
nicklas |
78 |
{ |
2722 |
11 Oct 06 |
nicklas |
79 |
if (shouldFail) |
2722 |
11 Oct 06 |
nicklas |
80 |
{ |
2722 |
11 Oct 06 |
nicklas |
81 |
response.setDone("Expected failure for command " + cmd + ": " + ex.getMessage()); |
2722 |
11 Oct 06 |
nicklas |
82 |
} |
2722 |
11 Oct 06 |
nicklas |
83 |
else |
2722 |
11 Oct 06 |
nicklas |
84 |
{ |
2722 |
11 Oct 06 |
nicklas |
85 |
response.setError("Unexpected failure for command " + cmd + ": " + ex.getMessage(), Arrays.asList(ex)); |
2722 |
11 Oct 06 |
nicklas |
86 |
} |
2722 |
11 Oct 06 |
nicklas |
87 |
return; |
2722 |
11 Oct 06 |
nicklas |
88 |
} |
2722 |
11 Oct 06 |
nicklas |
89 |
} |
2722 |
11 Oct 06 |
nicklas |
90 |
else if ("export".equals(cmd)) |
2722 |
11 Oct 06 |
nicklas |
91 |
{ |
2722 |
11 Oct 06 |
nicklas |
92 |
try |
2722 |
11 Oct 06 |
nicklas |
93 |
{ |
2722 |
11 Oct 06 |
nicklas |
94 |
int numNews = (Integer)job.getValue("numNews"); |
2722 |
11 Oct 06 |
nicklas |
95 |
exportNews(numNews); |
2722 |
11 Oct 06 |
nicklas |
96 |
} |
2722 |
11 Oct 06 |
nicklas |
97 |
catch (InvalidDataException ex) |
2722 |
11 Oct 06 |
nicklas |
98 |
{ |
2722 |
11 Oct 06 |
nicklas |
99 |
if (shouldFail) |
2722 |
11 Oct 06 |
nicklas |
100 |
{ |
2722 |
11 Oct 06 |
nicklas |
101 |
response.setDone("Expected failure for command " + cmd + ": " + ex.getMessage()); |
2722 |
11 Oct 06 |
nicklas |
102 |
} |
2722 |
11 Oct 06 |
nicklas |
103 |
else |
2722 |
11 Oct 06 |
nicklas |
104 |
{ |
2722 |
11 Oct 06 |
nicklas |
105 |
response.setError("Unexpected failure for command " + cmd + ": " + ex.getMessage(), Arrays.asList(ex)); |
2722 |
11 Oct 06 |
nicklas |
106 |
} |
2722 |
11 Oct 06 |
nicklas |
107 |
return; |
2722 |
11 Oct 06 |
nicklas |
108 |
} |
2722 |
11 Oct 06 |
nicklas |
109 |
} |
2722 |
11 Oct 06 |
nicklas |
110 |
if (shouldFail) |
2722 |
11 Oct 06 |
nicklas |
111 |
{ |
2722 |
11 Oct 06 |
nicklas |
112 |
response.setError("The permission test plugin executed succesfully, " + |
2722 |
11 Oct 06 |
nicklas |
113 |
"but we expected a failure for command: " + cmd, |
2722 |
11 Oct 06 |
nicklas |
114 |
null); |
2722 |
11 Oct 06 |
nicklas |
115 |
} |
2722 |
11 Oct 06 |
nicklas |
116 |
else |
2722 |
11 Oct 06 |
nicklas |
117 |
{ |
2722 |
11 Oct 06 |
nicklas |
118 |
response.setDone("The permission test plugin executed succesfully for command: " + cmd); |
2722 |
11 Oct 06 |
nicklas |
119 |
} |
2657 |
22 Sep 06 |
martin |
120 |
} |
2657 |
22 Sep 06 |
martin |
121 |
catch (BaseException ex) |
2657 |
22 Sep 06 |
martin |
122 |
{ |
2722 |
11 Oct 06 |
nicklas |
123 |
response.setError("Unexpected failure: " + ex.getMessage(), Arrays.asList(ex)); |
2657 |
22 Sep 06 |
martin |
124 |
} |
2657 |
22 Sep 06 |
martin |
125 |
} |
2657 |
22 Sep 06 |
martin |
126 |
|
2722 |
11 Oct 06 |
nicklas |
127 |
private void createNews(String description) |
2657 |
22 Sep 06 |
martin |
128 |
{ |
7962 |
04 Jun 21 |
nicklas |
129 |
DbControl dc = sc.newDbControl("Test: Permission plugin"); |
2657 |
22 Sep 06 |
martin |
130 |
try |
2657 |
22 Sep 06 |
martin |
131 |
{ |
2722 |
11 Oct 06 |
nicklas |
132 |
News n = News.getNew(dc, new Date(), new Date()); |
2722 |
11 Oct 06 |
nicklas |
133 |
n.setName("Permission test plugin"); |
2722 |
11 Oct 06 |
nicklas |
134 |
n.setDescription(description); |
2722 |
11 Oct 06 |
nicklas |
135 |
dc.saveItem(n); |
2657 |
22 Sep 06 |
martin |
136 |
dc.commit(); |
2657 |
22 Sep 06 |
martin |
137 |
} |
2657 |
22 Sep 06 |
martin |
138 |
finally |
2657 |
22 Sep 06 |
martin |
139 |
{ |
2722 |
11 Oct 06 |
nicklas |
140 |
if (dc != null) dc.close(); |
2657 |
22 Sep 06 |
martin |
141 |
} |
2657 |
22 Sep 06 |
martin |
142 |
} |
2657 |
22 Sep 06 |
martin |
143 |
|
2722 |
11 Oct 06 |
nicklas |
144 |
private void exportNews(int numNews) |
2657 |
22 Sep 06 |
martin |
145 |
{ |
7962 |
04 Jun 21 |
nicklas |
146 |
DbControl dc = sc.newDbControl("Test: Permission plugin"); |
2657 |
22 Sep 06 |
martin |
147 |
try |
2657 |
22 Sep 06 |
martin |
148 |
{ |
2722 |
11 Oct 06 |
nicklas |
149 |
List<News> news = new ArrayList<News>(News.getQuery().list(dc)); |
2722 |
11 Oct 06 |
nicklas |
150 |
if (news.size() != numNews) |
2722 |
11 Oct 06 |
nicklas |
151 |
{ |
2722 |
11 Oct 06 |
nicklas |
152 |
throw new InvalidDataException("Not allowed to read all news; expected " + numNews+ |
2722 |
11 Oct 06 |
nicklas |
153 |
"; got " + news.size()); |
2722 |
11 Oct 06 |
nicklas |
154 |
} |
2657 |
22 Sep 06 |
martin |
155 |
} |
2657 |
22 Sep 06 |
martin |
156 |
finally |
2657 |
22 Sep 06 |
martin |
157 |
{ |
2722 |
11 Oct 06 |
nicklas |
158 |
if (dc != null) dc.close(); |
2657 |
22 Sep 06 |
martin |
159 |
} |
2657 |
22 Sep 06 |
martin |
160 |
} |
2722 |
11 Oct 06 |
nicklas |
161 |
|
2657 |
22 Sep 06 |
martin |
162 |
} |