Esempio n. 1
0
	def deleteRule(self, ruleId, buildingName, roomName, editorUuid):
		checkData(locals())

		room = Room(buildingName = buildingName, roomName = roomName)
		room.retrieve()

		from app.backend.model.rule import Rule
		rule = Rule(id = ruleId)
		rule.retrieve()

		from app.backend.model.user import User
		editor = User(uuid = editorUuid)
		editor.retrieve()
		
		author = User(uuid = rule.authorUuid)
		author.retrieve()


		# If this room is not related to a room, I cannot modify it from this method
		if not rule.roomName:
			raise UserCredentialError("You cannot modify this group related rule.")

		# If this rule is not about this building
		if rule.buildingName != buildingName:
			raise UserCredentialError("You cannot modify the rule of another building.")			
			
		if author.uuid != editor.uuid:

			if not rule.groupId and not rule.getRoom().roomName in list(r.roomName for r in editor.getRooms()):
				raise UserCredentialError("You cannot modify a rule of a room you do not own.")				

			#if rule.getRoom() not in editor.getRooms():
			#	raise UserCredentialError("You cannot modify a rule of a room you do not own.")

			if rule.groupId:

				from app.backend.model.group import Group
				group = Group(buildingName = buildingName, id = groupId)
				group.retrieve()

				if not group:
					raise UserCredentialError("You cannot delete a rule of a room you do not own.")				

				if not group.crossRoomsValidation:
					raise UserCredentialError("You cannot delete a rule you do not own.")

				if not group.id in list(g.id for g in editor.getGroups()):
					raise UserCredentialError("You cannot delete a rule belonging to a group you do not own.")

				if not rule.getRoom().roomName in list(r.roomName for r in group.getRooms()):
					raise UserCredentialError("You cannot delete a rule belonging to room that does not belongs to a group you do not own.")

			if editor.level < rule.getPriority():
				raise UserCredentialError("You cannot modify this rule since it has a too high priority for your user level")			


		room.deleteRule(rule)

		return {}
Esempio n. 2
0
	def editRule(self, ruleId, priority, buildingName, roomName, editorUuid, groupId, ruleBody = None, antecedent = None, consequent = None, enabled = True):
		checkData(locals())


		if not ruleBody:
			if not antecedent: raise MissingInputDataError("")
			if not consequent: raise MissingInputDataError("")

		if ruleBody and antecedent: raise TooManyInputParametersError("")
		if ruleBody and consequent: raise TooManyInputParametersError("")


		from app.backend.model.rule import Rule
		oldRule = Rule(id = ruleId)
		oldRule.retrieve()

		from app.backend.model.user import User
		author = User(uuid = oldRule.authorUuid)
		author.retrieve()

		editor = User(uuid = editorUuid)
		editor.retrieve()

		writePermission = False
		errorMessage = ""


		# If this room is not related to a room, I cannot modify it from this method
		if not oldRule.roomName:
			raise UserCredentialError("You cannot modify this group related rule.")

		# If this rule is not about this building
		if oldRule.buildingName != buildingName:
			raise UserCredentialError("You cannot modify the rule of another building.")			
			
		if author.uuid != editor.uuid:

			if not groupId and not oldRule.getRoom().roomName in list(r.roomName for r in editor.getRooms()):
				raise UserCredentialError("You cannot modify a rule of a room you do not own.")				

			#if oldRule.getRoom() not in editor.getRooms():
			#	raise UserCredentialError("You cannot modify a rule of a room you do not own.")

			if groupId:

				from app.backend.model.group import Group
				group = Group(buildingName = buildingName, id = groupId)
				group.retrieve()

				if not group:
					raise UserCredentialError("You cannot modify a rule of a room you do not own.")				

				if not group.crossRoomsValidation:
					raise UserCredentialError("You cannot modify a rule you do not own.")

				if not group.id in list(g.id for g in editor.getGroups()):
					raise UserCredentialError("You cannot modify a rule belonging to a group you do not own.")

				if not oldRule.getRoom().roomName in list(r.roomName for r in group.getRooms()):
					raise UserCredentialError("You cannot modify a rule belonging to room that does not belongs to a group you do not own.")

			if editor.level < oldRule.getPriority():
				raise UserCredentialError("You cannot modify this rule since it has a too high priority for your user level")			

		authorUuid = editorUuid	

		result = self.__addOrModifyRule(ruleId = ruleId, priority = priority, buildingName = buildingName, roomName = roomName, authorUuid = authorUuid, ruleBody = ruleBody, antecedent = antecedent, consequent = consequent, enabled = enabled)		

		from app.backend.controller.notificationsManager import NotificationsManager
		notifications = NotificationsManager()
		messageSubject = "Rule modified in building " + str(buildingName) + " room " + str(roomName)
		messageText = "The user " + str(editor.username) + " edited (or tried to edit) the rule <<" + str(oldRule.getFullRepresentation()) + ">>. The new rule is <<" + str(ruleBody) + ">>"
		notifications.sendNotification(buildingName = buildingName, roomName = roomName, messageSubject = messageSubject, messageText = messageText) 

		return result