Example #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 {}
Example #2
0
	def enableRule(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()

		return self.editRule(ruleId = rule.id, priority = rule.getPriority(), buildingName = buildingName,  roomName = roomName, 
					editorUuid = editorUuid, groupId = rule.groupId, ruleBody = None, 
					antecedent = rule.antecedent, consequent = rule.consequent, enabled = True)
Example #3
0
	def __addOrModifyRule(self, priority = None, buildingName = None, roomName = None, authorUuid = None, ruleBody = None, ruleId = None, antecedent = None, consequent = None, enabled = True):
		checkData(locals())
		
		import time,datetime
		startTimeMilliseconds = long((time.time() + 0.5) * 1000)


		if ruleBody:
			try:
				antecedent = ruleBody.split("then")[0].replace("if ", "").strip()
				consequent = ruleBody.split("then")[1].strip()
			except Exception as e:
				raise NotWellFormedRuleError("There is a syntax error in the rule you are trying to save")

		
		# Detecting rule category (by the rule-consequent)
		from app.backend.controller.actionManager import ActionManager
		actionManager = ActionManager()
		category = actionManager.getAction(consequent).category
		
		from app.backend.model.user import User
		author = User(uuid = authorUuid)
		author.retrieve()

		if int(str(priority)) < 0 or int(str(priority)) > author.getMaxRoomPriority():
			raise RulePriorityError("You can specify rules for rooms with a priority value between 0 and " + str(author.getMaxRoomPriority()) + ". You inserted " + str(priority))



		from app.backend.model.rule import Rule
		from app.backend.model.room import Room

		if not ruleId:
			rule = Rule(priority = priority, category = category, buildingName = buildingName, roomName = roomName, authorUuid = authorUuid, antecedent = antecedent, consequent = consequent, enabled = True)
		else:
			rule = Rule(id = ruleId)
			rule.retrieve()
			author = rule.getAuthor()

			
			rule.antecedent = antecedent
			rule.consequent = consequent
			rule.authorUuid = authorUuid
			rule.authorUuid = authorUuid	
			rule.enabled = enabled		

			editor = rule.getAuthor()

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

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

		if not ruleId and not rule.checkIfUnique():
			raise DuplicatedRuleError("The submitted rule is already been saved for the considered room.")

		# excludedRuleId is needed to ignore the rule that the user want to edit
		excludedRuleId = ruleId if ruleId else None		

		roomRules = room.getRules(author = False, includeGroupsRules = True, excludedRuleId = excludedRuleId)

		# Checking that a priority is unique over the same category	
		for r in roomRules:
			if str(r.category) == str(category) and int(r.getPriority()) == int(priority):
				raise AlredyAssignedPriorityError("In room " + roomName + " the priority " + str(priority) + " has alredy been assigned to another rule with the same category!")

		temporaryRuleSet = []
		temporaryRuleSet.extend(roomRules)
		temporaryRuleSet.append(rule)

		#### OPTMIZATION ########################################################################################
		# Removing rules with a trigger that is different form the one of the rule I'm trying to insert or modify
		from app.backend.controller.triggerManager import TriggerManager
		triggerManager = TriggerManager()
		
		newRuleTriggers = triggerManager.translateTrigger(rule.antecedent)["triggers"]

		for i in range(0, len(temporaryRuleSet)):

			otherRuleTriggers = triggerManager.translateTrigger(temporaryRuleSet[i].antecedent)["triggers"]

			deleteRule = False

			if rule.category == temporaryRuleSet[i].category:
				for t1 in newRuleTriggers:
					for t2 in otherRuleTriggers:
						if t1 == t2:
							deleteRule = True
							break;
					if deleteRule: break;

			if deleteRule:
				del temporaryRuleSet[i]

		#### OPTMIZATION ########################################################################################


		for i in range(0, len(temporaryRuleSet)):
			temporaryRuleSet[i].groupId = None
			temporaryRuleSet[i].roomName = roomName


		from app.backend.controller.rulesetChecker import RulesetChecker
		rulesetChecker = RulesetChecker(temporaryRuleSet)
		ruleCheckErrorList = rulesetChecker.check()

		if len(ruleCheckErrorList) == 0:
			if ruleId: 
				rule.id = ruleId
				rule.setPriority(priority)


			from app.backend.commons.console import flash
			endTimeMilliseconds = long((time.time() + 0.5) * 1000)
			opTimeMilliseconds = endTimeMilliseconds - startTimeMilliseconds
			flash("RoomRuleVerification [SUCCESS]: roomName = " + str(roomName) +" #rules=" + str(len(temporaryRuleSet)) + " - opTimeMilliseconds:" + str(opTimeMilliseconds))


			return room.addRule(rule).getDict()
		else:
			from app.backend.commons.console import flash
			
			logMessage = "authorUuid =  " + str(authorUuid) + ", "
			logMessage += "buildingName =  " + str(buildingName) + ", "
			logMessage += "roomName =  " + str(roomName) + ", "
			logMessage += "ruleSetDescr =  " + str(temporaryRuleSet) + ", "
			logMessage += "newRule =  " + str(rule)

			flash("RuleValidationError: " + logMessage)

			endTimeMilliseconds = long((time.time() + 0.5) * 1000)
			opTimeMilliseconds = endTimeMilliseconds - startTimeMilliseconds
			flash("RoomRuleVerification [FAILED]: roomName = " + str(roomName) +" #rules=" + str(len(temporaryRuleSet)) + " - opTimeMilliseconds:" + str(opTimeMilliseconds))


			raise RuleValidationError(ruleCheckErrorList)
Example #4
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
Example #5
0
	def __addOrModifyRule(self, priority = None, buildingName = None, groupId = None, authorUuid = None, ruleBody = None, ruleId = None):
		checkData(locals())

		import time,datetime
		startTimeMilliseconds = long((time.time() + 0.5) * 1000)
		

		try:
			antecedent = ruleBody.split("then")[0].replace("if ", "").strip()
			consequent = ruleBody.split("then")[1].strip()
		except Exception as e:
			raise NotWellFormedRuleError("There is a syntax error in the rule you are trying to save")

		from app.backend.controller.actionManager import ActionManager
		actionManager = ActionManager()
		category = actionManager.getAction(consequent).category

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

		if int(str(priority)) < 0 or int(str(priority)) > author.getMaxGroupPriority():
			raise RulePriorityError("You can specify rules for groups with a priority value between 0 and " + str(author.getMaxRoomPriority()) + ". You inserted " + str(priority))

		from app.backend.model.rule import Rule
		from app.backend.model.group import Group

		if not ruleId:
			rule = Rule(priority = priority, category = category, buildingName = buildingName, groupId = groupId, authorUuid = authorUuid, antecedent = antecedent, consequent = consequent, enabled = True)
		else:
			rule = Rule(id = ruleId)
			rule.retrieve()
			author = rule.getAuthor()

			
			rule.antecedent = antecedent
			rule.consequent = consequent
			rule.authorUuid = authorUuid

			editor = rule.getAuthor()

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

		group = Group(buildingName = buildingName, id = groupId)
		group.retrieve()

		if not ruleId and not rule.checkIfUnique():
			raise DuplicatedRuleError("The submitted rule is already been saved for the considered group.")

		# excludedRuleId is needed to ignore the rule that the user want to edit
		excludedRuleId = ruleId if ruleId else None		


		ruleCheckErrorList = []
		groupRoomList = group.getRooms()
		for room in groupRoomList:

			groupRules = group.getRules(excludedRuleId = excludedRuleId)

			# Checking that a priority is unique over the same category	
			for r in groupRules:
				if str(r.category) == str(category) and int(r.getPriority()) == int(priority):
					raise AlredyAssignedPriorityError("In group " + str(groupId) + " the priority " + str(priority) + " has alredy been assigned to another rule with the same category!")


			temporaryRuleSet = []
			temporaryRuleSet.extend(room.getRules(author = False, includeGroupsRules = True, excludedRuleId = False, excludeCrossRoomValidationRules = True))
			temporaryRuleSet.extend(groupRules)

			temporaryRuleSet.append(rule)


			from app.backend.controller.rulesetChecker import RulesetChecker
			rulesetChecker = RulesetChecker(temporaryRuleSet)

			ruleCheckErrorList.extend(rulesetChecker.check())

		if len(ruleCheckErrorList) == 0:
			
			if ruleId: 	#if i'm in edit mode
				rule.id = ruleId
				rule.setPriority(priority)

			# Disabling rules in all the groups rooms since they have to be validated again

			for room in groupRoomList:
			
				from app.backend.controller.notificationsManager import NotificationsManager
				notifications = NotificationsManager()
				messageSubject = "Group " +  str(groupId) + " changed your room " + str(room.roomName) + " policy."
				messageText =  "Some rules in group " + str(groupId) + " have been changed."
				notifications.sendNotification(buildingName = buildingName, roomName = room.roomName, messageSubject = messageSubject, messageText = messageText) 
				
				#for r in room.getRules(includeGroupsRules = False, excludeCrossRoomValidationRules = True):
				#	r.disable()

			from app.backend.commons.console import flash
			endTimeMilliseconds = long((time.time() + 0.5) * 1000)
			opTimeMilliseconds = endTimeMilliseconds - startTimeMilliseconds
			flash("GroupsRuleVerification [SUCCESS]: groupId = " + str(groupId) +" #rules=" + str(len(temporaryRuleSet)) + " - opTimeMilliseconds:" + str(opTimeMilliseconds))

			return group.addRule(rule).getDict()
		else:

			from app.backend.commons.console import flash
			
			logMessage = "authorUuid =  " + str(authorUuid) + ", "
			logMessage += "buildingName =  " + str(buildingName) + ", "
			logMessage += "gropuId =  " + str(groupId) + ", "
			logMessage += "ruleSetDescr =  " + str(temporaryRuleSet) + ", "
			logMessage += "newRule =  " + str(rule)

			flash("RuleValidationError: " + logMessage)

			endTimeMilliseconds = long((time.time() + 0.5) * 1000)
			opTimeMilliseconds = endTimeMilliseconds - startTimeMilliseconds
			flash("GroupsRuleVerification [FAILED]: groupId = " + str(groupId) +" #rules=" + str(len(temporaryRuleSet)) + " - opTimeMilliseconds:" + str(opTimeMilliseconds))

			raise RuleValidationError(ruleCheckErrorList + " Error in room " + room.roomName)