8000 commit the missing parts · githubcs/baasbox@8dbcb7f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8dbcb7f

Browse files
committed
commit the missing parts
1 parent 6d501dd commit 8dbcb7f

File tree

7 files changed

+502
-229
lines changed

7 files changed

+502
-229
lines changed

.gitignore

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
!.gitignore
66

77
# Ignore Play! working directory #
8-
db
8+
/db
99
eclipse
1010
log
1111
logs
@@ -19,7 +19,7 @@ test-result
1919
server.pid
2020
*.iml
2121
*.eml
22-
dist
23-
doc
24-
script
25-
bin
22+
/dist
23+
/doc
24+
/script
25+
/bin
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright 2012-2013
3+
Claudio Tesoriero - c.tesoriero-at-baasbox.com
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.baasbox.db;
18+
19+
20+
import com.orientechnologies.orient.core.command.OCommandContext;
21+
import com.orientechnologies.orient.core.db.record.OIdentifiable;
22+
import com.orientechnologies.orient.core.record.impl.ODocument;
23+
import com.orientechnologies.orient.core.sql.OSQLEngine;
24+
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract;
25+
26+
public class CustomSqlFunctions {
27+
28+
public static void registerFunctions() {
29+
//registerCoalesce();
30+
}
31+
32+
public static void registerCoalesce(){
33+
OSQLEngine.getInstance().registerFunction("coalesce", new OSQLFunctionAbstract("coalesce", 1, 1000) {
34+
35+
36+
37+
@Override
38+
public String getSyntax() {
39+
return "Returns the first not-null parameter or null if all parameters are null. Syntax: coalesce(<field|value> [,<return_value_if_not_null>])";
40+
}
41+
42+
@Override
43+
public Object execute(OIdentifiable iCurrentRecord, ODocument iCurrentResult,
44+
final Object[] iParameters, OCommandContext iContext) {
45+
int length=iParameters.length;
46+
for (int i=0;i<length;i++){
47+
if (iParameters[i]!=null) return iParameters[i];
48+
}
49+
return null;
50+
}
51+
52+
});
53+
}
54+
55+
/*
56+
private static void registerExpand(){
57+
// REGISTER 'EXPAND' FUNCTION WITH FIXED 2 PARAMETERS
58+
OSQLEngine.getInstance().registerFunction("expand", new OSQLFunctionAbstract("expand", 2, 2) {
59+
public String getSyntax() {
60+
return "expand(<record>, <depth>)";
61+
}
62+
63+
64+
public boolean aggregateResults() {
65+
return false;
66+
}
67+
68+
@Override
69+
public Object execute(OIdentifiable currentObject, Object[] parameters,
70+
OCommandExecutor commandCaller) {
71+
72+
String depth = (String) parameters[1];
73+
74+
Object param = parameters[0];
75+
ODocument obj = null;
76+
if (param instanceof ORecordId){
77+
obj=DbHelper.getConnection().load((ORecordId)param);
78+
}else if (param instanceof ODocument){
79+
obj = (ODocument)param;
80+
}else if (param instanceof String){
81+
obj = ((ODocument)(currentObject.getRecord())).field((String)param);
82+
}else return null;
83+
if (obj==null) return null;
84+
ODocument ritorno = new ODocument();
85+
obj.copy (ritorno);
86+
return ritorno;
87+
}
88+
}); //OSQLEngine.getInstance().registerFunction
89+
}//registerExpand
90+
91+
*/
92+
}

app/com/baasbox/db/DbHelper.java

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*
2+
Copyright 2012-2013
3+
Claudio Tesoriero - c.tesoriero-at-baasbox.com
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.baasbox.db;
18+
19+
import java.io.FileInputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.util.List;
23+
24+
import org.apache.commons.io.IOUtils;
25+
26+
import play.Logger;
27+
import play.Play;
28+
import play.mvc.Http;
29+
30+
import com.baasbox.BBConfiguration;
31+
import com.baasbox.dao.DefaultRoles;
32+
import com.baasbox.db.hook.HooksManager;
33+
import com.baasbox.exception.SqlInjectionException;
34+
import com.baasbox.service.user.UserService;
35+
import com.baasbox.util.QueryParams;
36+
import com.orientechnologies.orient.core.command.OCommandRequest;
37+
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
38+
import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
39+
import com.orientechnologies.orient.core.db.record.ODatabaseRecordTx;
40+
import com.orientechnologies.orient.core.metadata.OMetadata;
41+
import com.orientechnologies.orient.core.metadata.security.ODatabaseSecurityResources;
42+
import com.orientechnologies.orient.core.metadata.security.ORole;
43+
import com.orientechnologies.orient.core.metadata.security.OUser;
44+
import com.orientechnologies.orient.core.record.impl.ODocument;
45+
import com.orientechnologies.orient.core.sql.OCommandSQL;
46+
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
47+
import com.orientechnologies.orient.core.tx.OTransactionNoTx;
48+
49+
50+
public class DbHelper {
51+
52+
private static final String SCRIPT_FILE_NAME="db.sql";
53+
private static final String fetchPlan = "*:?";
54+
55+
public static boolean isInTransaction(){
56+
OGraphDatabase db = getConnection();
57+
return !(db.getTransaction() instanceof OTransactionNoTx);
58+
}
59+
60+
public static void requestTransaction(){
61+
OGraphDatabase db = getConnection();
62+
if (!isInTransaction()){
63+
Logger.trace("Begin transaction");
64+
db.begin();
65+
}
66+
}
67+
68+
public static void commitTransaction(){
69+
OGraphDatabase db = getConnection();
70+
if (isInTransaction()){
71+
Logger.trace("Commit transaction");
72+
db.commit();
73+
}
74+
}
75+
76+
public static void rollbackTransaction(){
77+
OGraphDatabase db = getConnection();
78+
if (isInTransaction()){
79+
Logger.trace("Rollback transaction");
80+
db.rollback();
81+
}
82+
}
83+
84+
public static String selectQueryBuilder (String from, boolean count, QueryParams criteria){
85+
String ret;
86+
if (count) ret = "select count(*) from ";
87+
else ret = "select from ";
88+
ret += from;
89+
if (criteria.getWhere()!=null && !criteria.getWhere().equals("")){
90+
ret += " where ( " + criteria.getWhere() + " )";
91+
}
92+
if (!count && criteria.getOrderBy()!=null && !criteria.getOrderBy().equals("")){
93+
ret += " order by " + criteria.getOrderBy();
94+
}
95+
if (!count && (criteria.getPage()!=null && criteria.getPage()!=-1)){
96+
ret += " skip " + (criteria.getPage() * criteria.getRecordPerPage()) +
97+
" limit " + criteria.getRecordPerPage();
98+
}
99+
100+
Logger.debug("queryBuilder: " + ret);
101+
return ret;
102+
}
103+
104+
public static OCommandRequest selectCommandBuilder(String from, boolean count, QueryParams criteria) throws SqlInjectionException{
105+
OGraphDatabase db = DbHelper.getConnection();
106+
OCommandRequest command = db.command(new OSQLSynchQuery<ODocument>(
107+
selectQueryBuilder(from, count, criteria)
108+
).setFetchPlan(fetchPlan.replace("?", criteria.getDepth().toString())));
109+
if (!command.isIdempotent()) throw new SqlInjectionException();
110+
Logger.debug("commandBuilder: ");
111+
Logger.debug(" " + criteria.toString());
112+
Logger.debug(" " + command.toString());
113+
return command;
114+
}
115+
116+
public static List<ODocument> commandExecute(OCommandRequest command, String[] params){
117+
List<ODocument> queryResult = command.execute((Object[])params);
118+
return queryResult;
119+
}
120+
121+
public static OGraphDatabase open(String username,String password){
122+
OGraphDatabase db=new OGraphDatabase("local:db/baasbox").open(username, password);
123+
HooksManager.registerAll(db);
124+
return db;
125+
}
126+
127+
public static OGraphDatabase getConnection(){
128+
return new OGraphDatabase ((ODatabaseRecordTx)ODatabaseRecordThreadLocal.INSTANCE.get());
129+
}
130+
131+
public static String getCurrentHTTPPassword(){
132+
return (String) Http.Context.current().args.get("password");
133+
}
134+
135+
public static String getCurrentHTTPUsername(){
136+
return (String) Http.Context.current().args.get("username");
137+
}
138+
139+
public static String getCurrentUserName(){
140+
return getConnection().getUser().getName();
141+
}
142+
143+
public static boolean isConnectedLikeBaasBox(){
144+
return getCurrentHTTPUsername().equalsIgnoreCase(BBConfiguration.getBaasBoxUsername());
145+
}
146+
147+
public static void createDefaultRoles(){
148+
Logger.trace("Method Start");
149+
OGraphDatabase db = DbHelper.getConnection();
150+
final ORole anonymousUserRole = db.getMetadata().getSecurity().createRole(DefaultRoles.ANONYMOUS_USER.toString(), ORole.ALLOW_MODES.DENY_ALL_BUT);
151+
anonymousUserRole.save();
152+
final ORole registeredUserRole = db.getMetadata().getSecurity().createRole(DefaultRoles.REGISTERED_USER.toString(), ORole.ALLOW_MODES.DENY_ALL_BUT);
153+
registeredUserRole.save();
154+
155+
final ORole backOfficeRole = db.getMetadata().getSecurity().createRole(DefaultRoles.BACKOFFICE_USER.toString(),ORole.ALLOW_MODES.DENY_ALL_BUT);
156+
backOfficeRole.save();
157+
158+
registeredUserRole.addRule(ODatabaseSecurityResources.DATABASE, ORole.PERMISSION_READ);
159+
registeredUserRole.addRule(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_READ + ORole.PERMISSION_CREATE
160+
+ ORole.PERMISSION_UPDATE);
161+
registeredUserRole.addRule(ODatabaseSecurityResources.CLUSTER + "." + OMetadata.CLUSTER_INTERNAL_NAME, ORole.PERMISSION_READ);
162+
registeredUserRole.addRule(ODatabaseSecurityResources.CLUSTER + ".orole", ORole.PERMISSION_READ);
163+
registeredUserRole.addRule(ODatabaseSecurityResources.CLUSTER + ".ouser", ORole.PERMISSION_READ);
164+
registeredUserRole.addRule(ODatabaseSecurityResources.ALL_CLASSES, ORole.PERMISSION_ALL);
165+
registeredUserRole.addRule(ODatabaseSecurityResources.ALL_CLUSTERS, ORole.PERMISSION_ALL);
166+
registeredUserRole.addRule(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_ALL);
167+
registeredUserRole.addRule(ODatabaseSecurityResources.RECORD_HOOK, ORole.PERMISSION_ALL);
168+
169+
170+
backOfficeRole.addRule(ODatabaseSecurityResources.DATABASE, ORole.PERMISSION_READ);
171+
backOfficeRole.addRule(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_READ + ORole.PERMISSION_CREATE
172+
+ ORole.PERMISSION_UPDATE);
173+
backOfficeRole.addRule(ODatabaseSecurityResources.CLUSTER + "." + OMetadata.CLUSTER_INTERNAL_NAME, ORole.PERMISSION_READ);
174+
backOfficeRole.addRule(ODatabaseSecurityResources.CLUSTER + ".orole", ORole.PERMISSION_READ);
175+
backOfficeRole.addRule(ODatabaseSecurityResources.CLUSTER + ".ouser", ORole.PERMISSION_READ);
176+
backOfficeRole.addRule(ODatabaseSecurityResources.ALL_CLASSES, ORole.PERMISSION_ALL);
177+
backOfficeRole.addRule(ODatabaseSecurityResources.ALL_CLUSTERS, ORole.PERMISSION_ALL);
178+
backOfficeRole.addRule(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_ALL);
179+
backOfficeRole.addRule(ODatabaseSecurityResources.RECORD_HOOK, ORole.PERMISSION_ALL);
180+
backOfficeRole.addRule(ODatabaseSecurityResources.BYPASS_RESTRICTED,ORole.PERMISSION_ALL); //the backoffice users can access and manipulate all records
181+
182+
anonymousUserRole.addRule(ODatabaseSecurityResources.DATABASE, ORole.PERMISSION_READ);
183+
anonymousUserRole.addRule(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_READ);
184+
anonymousUserRole.addRule(ODatabaseSecurityResources.CLUSTER + "." + OMetadata.CLUSTER_INTERNAL_NAME, ORole.PERMISSION_READ);
185+
anonymousUserRole.addRule(ODatabaseSecurityResources.CLUSTER + ".orole", ORole.PERMISSION_READ);
186+
anonymousUserRole.addRule(ODatabaseSecurityResources.CLUSTER + ".ouser", ORole.PERMISSION_READ);
187+
anonymousUserRole.addRule(ODatabaseSecurityResources.ALL_CLASSES, ORole.PERMISSION_READ);
188+
anonymousUserRole.addRule(ODatabaseSecurityResources.ALL_CLUSTERS, ORole.PERMISSION_READ);
189+
anonymousUserRole.addRule(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);
190+
anonymousUserRole.addRule(ODatabaseSecurityResources.RECORD_HOOK, ORole.PERMISSION_READ);
191+
192+
anonymousUserRole.save();
193+
registeredUserRole.save();
194+
Logger.trace("Method End");
195+
}
196+
197+
public static void createDefaultUsers() throws Exception{
198+
Logger.trace("Method Start");
199+
//the baasbox default user used to connect to the DB like anonymous user
200+
String username=BBConfiguration.getBaasBoxUsername();
201+
String password=BBConfiguration.getBaasBoxPassword();
202+
UserService.signUp(username, password, null,null,null,null);
203+
Logger.trace("Method End");
204+
}
205+
206+
public static void dropOrientDefault(){
207+
Logger.trace("Method Start");
208+
OGraphDatabase db = DbHelper.getConnection();
209+
db.getMetadata().getSecurity().dropUser("reader");
210+
db.getMetadata().getSecurity().dropUser("writer");
211+
db.getMetadata().getSecurity().dropRole("reader");
212+
db.getMetadata().getSecurity().dropRole("writer");
213+
Logger.trace("Method End");
214+
}
215+
216+
public static void populateDB(OGraphDatabase db) throws IOException{
217+
Logger.info("Populating the db...");
218+
InputStream is;
219+
if (Play.application().isProd()) is =Play.application().resourceAsStream(SCRIPT_FILE_NAME);
220+
else is = new FileInputStream(Play.application().getFile("conf/"+SCRIPT_FILE_NAME));
221+
List<String> script=IOUtils.readLines(is, "UTF-8");
222+
223+
for (String line:script){
224+
Logger.debug(line);
225+
if (!line.startsWith("--") && !line.trim().isEmpty()){ //skip comments
226+
db.command(new OCommandSQL(line.replace(';', ' '))).execute();
227+
}
228+
}
229+
Logger.info("...done");
230+
}
231+
232+
233+
}

0 commit comments

Comments
 (0)
0