summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/server/ServerHandler.java
blob: 284f959cfd57cd53b0ef5fcc29309568e194a7a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
package server;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import models.Configuration;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;

import server.generated.Image;
import server.generated.Lecture;
import server.generated.Person;
import server.generated.Server;
import server.generated.User;

import org.openslx.imagemaster.thrift.iface.ImageServer.Client;
import org.openslx.imagemaster.thrift.iface.InvalidTokenException;
import org.openslx.imagemaster.thrift.iface.UserInfo;

import thrift.MasterThriftConnection;
//import thrift.SessionData;

import sql.SQL;

//import util.XMLCreator;

public class ServerHandler implements Server.Iface {

	private static Logger log = Logger.getLogger(ServerHandler.class);
	static SQL sql = new SQL();
	
	private Map<String,UserInfo> tokenManager = new HashMap<>(); //saves the current tokens and the mapped userdata, returning from the server


	public boolean authenticated(String token) throws TException
	{	
		if(tokenManager.get(token) != null)
		{
			//user found in tokenManager, session was set to valid once before (cached session, no further action needed)
			return true;
		}
		else
		{
			MasterThriftConnection thrift = new MasterThriftConnection();
			Client client = thrift.getMasterThriftConnection();

			//user not in tokenManager, check authentication, then add user to tokenManager
			log.info("token is: "+token);
			UserInfo ui = null;
			if( (ui = client.getUserFromToken(token)) != null) //user authenticated by masterserver
			{
				//user was authenticated by the masterserver, cache the data
				tokenManager.put(token, ui);
				return true;
			}
		}
		
		return false;
	}
	
	
	private UserInfo getUserFromToken(String token) //local function, which gets userdata from the tokenmanager, not the masterserver
	{											   //implemented, as there is no need for userdata in each function, so return type of authenticated should stay boolean
		UserInfo ui = tokenManager.get(token);
		return ui;
	}
	
	
	public boolean setSessionInvalid(String token)
	{
		log.info("token disabling.. round one");
		log.info(tokenManager.get(token));
		
		
		tokenManager.remove(token);
		
		log.info("token disabling.. round two");
		log.info(tokenManager.get(token));
		
		if(tokenManager.get(token) == null) //check if deletion worked and token isn't stored anymore
		{
			return true;
		}
		return false;

	}

	@Override
	public User getFtpUser(String token) throws TException 
	{
		if(authenticated(token))
		{

			log.info("returning FTPUser...");
			User user = new User();
			user.setUserName(UUID.randomUUID().toString().substring(0, 8));
			user.setPassword(getEncodedSha1Sum(UUID.randomUUID().toString()
					.substring(0, 8)));
			if (Configuration.config.getAbsolute_path().endsWith("/")) {
				user.setPath(Configuration.config.getAbsolute_path());
			} else {
				user.setPath(Configuration.config.getAbsolute_path() + "/");
			}

			// check if folder temp and folder prod exist
			if (folderTempExists() == true && folderProdExists() == true) {
				sql.writeFTPUser(user.getUserName(), user.getPassword());
				return user;
			} else {
				log.info("Error: returning null user");
				return null;
			}
		}
		return null;

	}

	public boolean folderTempExists() {
		// check if folder temp exists, otherwise create it
		Path path = null;
		if (Configuration.config.getAbsolute_path().endsWith("/")) {
			path = Paths.get(Configuration.config.getAbsolute_path() + "temp");
		} else {
			path = Paths.get(Configuration.config.getAbsolute_path() + "/temp");
		}

		if (Files.exists(path) == true) {
			log.info("folder '" + path + "' exists, no further action");
			return true;
		} else {
			// create directory and set permissions
			boolean success = (new File(path + "")).mkdirs();

			if (!success) {
				log.info("failed to create folder '" + path + "'");
				return false;
			} else {
				// set permissions
				try {
					Runtime.getRuntime().exec("chmod 777 " + path);
				} catch (IOException e) {
					e.printStackTrace();
				}
				log.info("folder '" + path + "' successfully created");
				return true;
			}
		}

	}// end folderTempExists()

	public boolean folderProdExists() {
		// check if folder temp exists, otherwise create it
		Path path = null;
		if (Configuration.config.getAbsolute_path().endsWith("/")) {
			path = Paths.get(Configuration.config.getAbsolute_path() + "prod");
		} else {
			path = Paths.get(Configuration.config.getAbsolute_path() + "/prod");
		}

		if (Files.exists(path) == true) {
			log.info("folder '" + path + "' exists, no further action");
			return true;
		} else {
			// create directory and set permissions
			boolean success = (new File(path + "")).mkdirs();

			if (!success) {
				log.info("failed to create folder '" + path + "'");
				return false;
			} else {
				// set permissions
				try {
					Runtime.getRuntime().exec("chmod 777 " + path);
				} catch (IOException e) {
					e.printStackTrace();
				}
				log.info("folder '" + path + "' successfully created");
				return true;
			}
		}

	}// end folderProdExists()

	public String getEncodedSha1Sum(String key) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(key.getBytes());
			log.info("successfully returned EncodedSha1Sum");
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			// handle error case to taste
		}
		return null;
	}

	
	@Override

	public long DeleteFtpUser(String user, String token) throws TException 
	{
		if(authenticated(token))
		{

			return sql.DeleteUser(user);
		}
		return -1;
	}

	
	@Override

	public String getPathOfImage(String image_id, String version, String token) throws TException 
	{
		if(authenticated(token))
		{
			log.info("successfully returned PathOfImage: " + sql.getPathOfImage(image_id, version));

			return sql.getPathOfImage(image_id, version);
		}
		return null;
	}

	@Override
	public String setInstitution(String university, String token) throws TException 
	{
		if(authenticated(token))
		{

			return sql.setInstitution(university);
		}
		return null;
	}

	@Override
	public boolean writeVLdata(String imagename, String desc,
			String Tel, String Fak, boolean license, boolean internet,
			long ram, long cpu, String imagePath, boolean isTemplate,
			long filesize, long shareMode, String os, String uid, String token, String userID) throws TException 
	{
		
		if(authenticated(token))
		{
			String mode = null;
			
	
			if (shareMode == 0) {
				mode = "only_local";
			} else {
				mode = "to_be_published";
			}

			// String pk_institution = sql.setInstitution(university);
			// String pk_person = sql.setPerson(login, lastname, firstname,
			// Mail,
			// new Date(), pk_institution);

			// OS impl Select and write
			// ACHTUNG: Anzahl der Leerzeichen muss eingehalten werden: 'Windows
			// 7
			// 32 bit"
			String pk_os = sql.getOSpk(os.substring(0, nthIndexOf(os, " ", 2)),
					os.substring(nthIndexOf(os, " ", 2), os.lastIndexOf(" "))
							.replace(" ", ""));

			// sql.setImageData(pk_person, license, internet, cpu, ram,
			// imagename,desc, imagePath, filesize,mode,pk_os);

			sql.setImageData(userID, license, internet, cpu, ram, imagename, desc,
					imagePath, filesize, mode, pk_os, uid);
	
			log.info("userID in serverhandler was: "+userID);

			log.info("written VLdata");
			return true;
		}
		return false;
	}

	@Override

	public List<Image> getImageListPermissionWrite(String userID, String token) throws TException 
	{
		if(authenticated(token))
		{

			return sql.getImageListPermissionWrite(userID);
		}
		return null;
	}

	@Override
	public List<Image> getImageListPermissionRead(String userID, String token) throws TException 
	{
		if(authenticated(token))
		{

			return sql.getImageListPermissionRead(userID);
		}
		return null;
	}

	@Override
	public List<Image> getImageListPermissionLink(String userID, String token) throws TException 
	{
		if(authenticated(token))
		{

			return sql.getImageListPermissionLink(userID);
		}
		return null;
	}

	@Override
	public List<Image> getImageListPermissionAdmin(String userID, String token) throws TException 
	{
		if(authenticated(token))
		{

			return sql.getImageListPermissionAdmin(userID);
		}
		return null;
	}

	@Override
	public List<Image> getImageListAllTemplates(String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getImageListAllTemplates();
		}
		return null;
	}

	@Override
	public List<String> getAllOS(String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getAllOS();
		}
		return null;
	}

	//UserInfo does not return the institution, so in this case, the local method is prepared, but not yet executed, as the institution has to be added to UserInfo (or selected by institutionID)
	@Override
	public Map<String, String> getPersonData(String Vorname, String Nachname, String token) throws TException 
	{
		
		if(authenticated(token))
		{

		Map<String, String> map = new HashMap<>();
		
		UserInfo ui = getUserFromToken(token);
		
		map.put("mail", ui.getEMail());
		map.put("Nachname", ui.getLastName());
		map.put("Vorname", ui.getFirstName());
		
		//map.put("Hochschule", sql.getInstitutionByID(ui.getOrganizationId())); //does not deliver the correct id
		Map<String, String> tempMap = new HashMap<>();
		tempMap = sql.getPersonData(Vorname, Nachname);
		map.put("Hochschule", tempMap.get("Hochschule"));
		
		return map;
		}
		return null;
	}

	
	public void setPerson(String userID, String token, String institution) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			//String institution = sql.getInstitutionByID(ui.getOrganizationId());
			sql.setPerson(userID, ui.getLastName(), ui.getFirstName(), ui.getEMail(), new Date(), institution);
		}
	}
	
	

	@Override
	public boolean writeLecturedata(String name, String shortdesc, String desc,
			String startDate, String endDate, boolean isActive,
			String imageID, String token, String Tel, String Fak, String lectureID, String university)
			throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
		
			//String pk_image = imageID;
			Map<String, String> map = new HashMap<String, String>();
			int imageversion = 0;
			//String university = sql.getInstitutionByID(ui.getOrganizationId());
			String pk_institution = sql.setInstitution(university);
			String pk_person = sql.setPerson(ui.getEMail(), ui.getLastName(), ui.getFirstName(), ui.getEMail(),
					new Date(), pk_institution);

			map = sql.getImageIDandVersion(imageID);

			// pk_image = map.get("GUID");
			imageversion = Integer.parseInt(map.get("version"));

			sql.setLectureData(pk_person, imageID, imageversion, name, desc,
					shortdesc, startDate, endDate, isActive, lectureID);
		}
		return false;

	}

	@Override
	public boolean startFileCopy(String filename, String token) throws TException 
	{
		if(authenticated(token))
		{
			// copy file from folder temp to folder prod
			String file = Configuration.config.getAbsolute_path() + "temp/"
					+ filename;
			File tmpFile = new File(file);

			log.info("Trying to move file to '/srv/openslx/nfs/prod/"
					+ tmpFile.getName() + "'");
			try {
				FileUtils.moveFile(tmpFile,
						new File(Configuration.config.getAbsolute_path()
								+ "prod/" + filename));
				// int ret = sql.UpdateImagePath(filename);
				if (sql.UpdateImagePath(filename) == 0) {
					log.info("file moved and database updated.");
				}

			} catch (IOException e) {
				log.info("Failed to move file.");
				e.printStackTrace();
			}
		}
		return true;
	}

	@Override
	public Map<String, String> getImageData(String imageid, String imageversion, String token) throws TException 
	{
		if(authenticated(token))
		{
			//log.info("returning ImageData: "+ sql.getImageData(imageid, imageversion).size() + "items.");
			return sql.getImageData(imageid, imageversion);
		}
		return null;
	}

	@Override
	public boolean updateImageData(String name, String newName, String desc,
			String image_path, boolean license, boolean internet, long ram,
			long cpu, String id, String version, boolean isTemplate,
			long filesize, long shareMode, String os, String token) throws TException 
	{

		if (authenticated(token)) 
		{
			
			//get old_image_path
			String old_image_path = sql.getFile(id, version);
			//log.debug("old file path has value:"+old_image_path.substring(5));
			//log.debug("new file path has value:"+image_path.substring(5));


			String mode = null;

			if (shareMode == 0) {
				mode = "only_local";
			} else {
				mode = "to_be_published";
			}
			String pk_os = sql.getOSpk(os.substring(0, nthIndexOf(os, " ", 2)),
					os.substring(nthIndexOf(os, " ", 2), os.lastIndexOf(" "))
							.replace(" ", ""));

			// do database update - if successful then delete old file from
			// drive
			int val = sql.UpdateImageData(name, newName, desc, image_path,
					license, internet, cpu, ram, id, version, isTemplate,
					filesize, mode, pk_os);
			
			

			//check if new file has been uploaded by checking if the new file path equals the old file path
			//if so, no new file was uploaded. Else delete old file
			if (val == 0 && (!old_image_path.substring(5).matches(image_path.substring(5))) ) {
				// update was successful - delete old file
				//log.debug("deleting file "+old_image_path);
				deleteImageByPath(old_image_path);
			} else {
				// update was not successful - delete new file
				// TODO not yet implemented
				//log.debug("doing nothing because no new file was uploaded..");

			}
		}
		return false;
	}
	

	@Override
	public List<Lecture> getLectureList(String token) throws TException 
	{
		if(authenticated(token))
		{
			//log.info("returning LectureList");
			return sql.getLectureList();
		}
		return null;
	}

	@Override
	public List<Lecture> getLectureListPermissionRead(String token) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			//log.info("returning LectureListRead");
			return sql.getLectureListPermissionRead(ui.getUserId());
		}
		return null;
	}// end getLectureListPermissionRead

	@Override
	public List<Lecture> getLectureListPermissionWrite(String token) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			//log.info("returning LectureListWrite");
			return sql.getLectureListPermissionWrite(ui.getUserId());
		}
		return null;
	}// end getLectureListPermissionRead

	@Override

	public List<Lecture> getLectureListPermissionAdmin(String token) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			//log.info("returning LectureListAdmin");
			return sql.getLectureListPermissionAdmin(ui.getUserId());
		}
		return null;
	}// end getLectureListPermissionRead

	
	@Override
	public boolean updateLecturedata(String name, String newName,
			String shortdesc, String desc, String startDate, String endDate,
			boolean isActive, String imageid, String imageversion, String token,
			String Tel, String Fak, String id, String university) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			
			sql.updateLectureData(imageid, imageversion, ui.getLastName(), newName, desc,
					shortdesc, startDate, endDate, isActive, id);
			
		}
		return false;
	}

	@Override
	public boolean deleteImageServer(String imageid, String imageversion, String token) throws TException 
	{
		if(authenticated(token))
		{
			String stringFile = sql.getFile(imageid, imageversion);
			log.info("File to Delete: " + stringFile);

			File tmpFile = new File(Configuration.config.getAbsolute_path()
					+ stringFile);
			
			log.info("Absolute Path used for deletion: "+tmpFile);

			try {
				// File wird von Server gelöscht
				FileUtils.forceDelete(tmpFile);
				return true;

			} catch (IOException e) {
				log.info("Failed to execute deleteImageServer.");
				e.printStackTrace();

			}
		}
		return false;
	}
	
	@Override
	public boolean deleteImageData(String id, String version, String token) throws TException 
	{
		boolean success=false;
		
		if(authenticated(token))
		{
			if(sql.deleteImage(id, version)==true)
			{
				success=true;
				log.info("Image '"+id+"' and permissions successfully deleted.");
			}
		}
		return success;
	}

//TODO
public boolean deleteImageByPath(String image_path) throws TException{
	


			//String stringFile = sql.getFile(imageid, imageversion);
			log.info("File to Delete: " + image_path);

			File tmpFile = new File(Configuration.config.getAbsolute_path()
					+ image_path);

			try {
				// File wird von Server gelöscht
				FileUtils.forceDelete(tmpFile);
				return true;

			} catch (IOException e) {
				log.info("Failed to execute deleteImageServer.");
				e.printStackTrace();

			}
		

	return false;
	
}

	@Override
	public boolean connectedToLecture(String id, String version, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.connectedToLecture(id, version);
		}
		return true;
	}

	public boolean deleteLecture(String id, String token, String university) throws TException 
	{
		boolean success = false;
		
		UserInfo ui = getUserFromToken(token);
		
		String user = ui.getEMail();
		
		if(authenticated(token))
		{
			if(sql.deleteLecture(id) == true)
			{

				success = true;
				log.info("Lecture '" + id
						+ "' and permissions successfully deleted.");
			}
		}
		return success;

	}

	@Override
	public List<String> getAllUniversities(String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getAllUniversities();
		}
		return null;
	}

	@Override
	public Map<String, String> getLectureData(String lectureid, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getLectureData(lectureid);
		}
		return null;
	}

	public static int nthIndexOf(final String string, final String searchToken,final int index) 
	{
		int j = 0;

		for (int i = 0; i < index; i++) 
		{
			j = string.indexOf(searchToken, j + 1);

			if (j == -1)
				break;
		}

		return j;
	}

	@Override
	public boolean checkUser(String username, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.checkUser(username);
		}
		return false;

	}

	@Override
	public boolean createUser(String token, String university) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			String pk_institution = sql.setInstitution(university);
			String pk_person = sql.setPerson(ui.getEMail(), ui.getLastName(), ui.getFirstName(), ui.getEMail(), new Date(), pk_institution);
			return true;
		}
		return false;
	}

	@Override
	public boolean writeImageRights(String imageID, String token,
			String role, String university, String userID) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			String pk_image = null;
			Map<String, String> map = new HashMap<String, String>();
		
			String pk_institution = sql.setInstitution(university);

			String pk_person = sql.setPerson(userID, ui.getLastName(), ui.getFirstName(), ui.getEMail(), new Date(), pk_institution);
	
			map = sql.getImageIDandVersion(imageID);

			pk_image = map.get("GUID");

			if (role.equals("Dozent")) {
				int read = 1;
				int write = 1;
				// int changePermission=0;
				int admin = 1;
				int linkallowed = 1;
				int roleID = sql.getRoleID(role);

				sql.setImageRights(pk_person, pk_image, roleID, read, write,
						admin, linkallowed);

			} else if (role.equals("Admin")) {
				int read = 1;
				int write = 1;
				// int changePermission=1;
				int admin = 1;
				int linkallowed = 1;
				int roleID = sql.getRoleID(role);

				sql.setImageRights(pk_person, pk_image, roleID, read, write,
						admin, linkallowed);
			} else {
				int read = 1;
				int write = 0;
				// int changePermission=0;
				int admin = 0;
				int linkallowed = 0;
				int roleID = sql.getRoleID(role);

				sql.setImageRights(pk_person, pk_image, roleID, read, write,
						admin, linkallowed);
			}

			log.info("Written image rights");
			return true;
		}
		return false;
	}

	@Override
	public boolean writeLectureRights(String lectureID, String role, String token, String university, String userID) throws TException 
	{
		if(authenticated(token))
		{
			//String pk_lecture = null;
			UserInfo ui = getUserFromToken(token);
			String pk_institution = sql.setInstitution(university);
			String pk_person = sql.setPerson(userID, ui.getLastName(), ui.getFirstName(), ui.getEMail(), new Date(), pk_institution);
			//pk_lecture = sql.getLectureID(lectureID);
	
			if (role.equals("Dozent")) {
				int read = 1;
				int write = 1;
				// int changePermission=0;
				int admin = 1;
				int roleID = sql.getRoleID(role);

				sql.setLectureRights(pk_person, lectureID, roleID, read, write,
						admin);

			} else if (role.equals("Admin")) {
				int read = 1;
				int write = 1;
				// int changePermission=1;
				int admin = 1;
				int roleID = sql.getRoleID(role);

				sql.setLectureRights(pk_person, lectureID, roleID, read, write,
						admin);
			} else {
				int read = 0;
				int write = 0;
				// int changePermission=0;
				int admin = 0;
				int roleID = sql.getRoleID(role);

				sql.setLectureRights(pk_person, lectureID, roleID, read, write,
						admin);
			}

			return true;
		}
		return false;
	}

	@Override
	public List<Person> getAllOtherSatelliteUsers(List<String> userID, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getAllOtherSatelliteUsers(userID);
			// return null;
		}
		return null;
	}

	// set permissions for users which are !=userID
	public boolean writeAdditionalImageRights(String imageID, String userID,
			boolean isRead, boolean isWrite, boolean isLinkAllowed,
			boolean isAdmin, String token) throws TException 
	{
		boolean success = false;
		if(authenticated(token))
		{
			Map<String, String> map = new HashMap<String, String>();
			map = sql.getImageIDandVersion(imageID);
			// String imageID = map.get("GUID");

			sql.writeAdditionalImageRights(imageID, userID, isRead, isWrite,
					isLinkAllowed, isAdmin);
			log.info("Written additional image rights for " + userID);
		}
		return success;
	}

	public boolean writeAdditionalLectureRights(String lectureID,
			String userID, boolean isRead, boolean isWrite, boolean isAdmin, String token) throws TException 
	{
		if(authenticated(token))
		{
			Map<String, String> map = new HashMap<String, String>();
			// String lectureID = sql.getLectureID(lectureID);

			sql.writeAdditionalLectureRights(lectureID, userID, isRead,
					isWrite, isAdmin);
			log.info("Written additional lecture rights for " + userID);

			return true;
		}
		return false;
	}

	@Override
	public List<Person> getPermissionForUserAndImage(String token,
			String imageID, String userID) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getPermissionForUserAndImage(userID, imageID);
		}
		return null;
	}


	public List<Person> getPermissionForUserAndLecture(String token,String lectureID, String userID) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			return sql.getPermissionForUserAndLecture(userID, lectureID);
		}
		return null;
	}

	@Override
	public void deleteAllAdditionalImagePermissions(String imageID, String token, String userID) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			sql.deleteAllAdditionalImagePermissions(imageID, userID);
		}
		return;
	}

	@Override
	public void deleteAllAdditionalLecturePermissions(String lectureID,String token, String userID) throws TException 
	{
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			sql.deleteAllAdditionalLecturePermissions(lectureID, userID);
		}

		return;
	}


	@Override 
	public List<Image> getImageList(String userID, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getImageList(userID);
		}
		return null;
	}

	@Override
	public List<String> getAdditionalImageContacts(String imageID, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getAdditionalImageContacts(imageID);
		}
		return null;
	}

	@Override
	public String getOsNameForGuestOs(String guestOS, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getOsNameForGuestOs(guestOS);
		}
		return null;
	}

	@Override
	public String createRandomUUID(String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.createRandomUUID();
		}
		return null;
	}

	public Map<String, String> getItemOwner(String itemID, String token) throws TException 
	{
		if(authenticated(token))
		{
			return sql.getItemOwner(itemID);
		}
		return null;

	}

	@Override
	public boolean userIsImageAdmin(String imageID, String token, String userID)
			throws TException {
		
		if(authenticated(token))
		{
			UserInfo ui = getUserFromToken(token);
			return sql.userIsImageAdmin(userID,imageID);
		}
		return false;

	}

	@Override
	public boolean userIsLectureAdmin(String userID, String lectureID, String token)
			throws TException {

		if(authenticated(token))
		{
			return sql.userIsLectureAdmin(userID,lectureID);

		}
		return false;
	}


	@Override
	public String getInstitutionByID(String institutionID) throws TException {
		// TODO Auto-generated method stub
		return null;
	}



}// end class