public IActionResult GetAttribute([FromBody] GetAttributeRequest value) { var session = this.GetSession(value.Session, true); T2D.Entities.BaseThing thing = this.Find(value.ThingId) .Include(t => t.ThingAttributes) .FirstOrDefault() ; if (thing == null) { return(BadRequest($"Thing '{value.ThingId}' do not exists.")); } var role = this.RoleMapper.EnumToEntity(value.Role); var attribute = this.AttributeMapper.EnumToEntity(value.Attribute); GetAttributeResponse ret = new GetAttributeResponse { Attribute = attribute.Name, TimeStamp = DateTime.UtcNow, Value = GetPropertyValue(thing, attribute.Name) }; return(Ok(ret)); }
public IActionResult GetRelations([FromBody] GetRelationsRequest value) { var session = this.GetSession(value.Session, true); T2D.Entities.BaseThing thing = this.Find(value.ThingId) .Include(t => t.ThingAttributes) .Include(t => t.ThingRelations) .FirstOrDefault() ; if (thing == null) { return(BadRequest($"Thing '{value.ThingId}' do not exists.")); } var role = this.RoleMapper.EnumToEntity(value.Role); //TODO: check that session has right to if (!AttributeSecurity.QueryRelationsRight(thing, session, role)) { return(BadRequest($"Not enough priviledges to query relations for thing {value.ThingId}.")); } var ret = new GetRelationsResponse(); ret.RelationThings = new List <GetRelationsResponse.RelationsThings>(); foreach (var group in thing.ThingRelations.GroupBy(tr => tr.RelationId)) { var rt = new GetRelationsResponse.RelationsThings { Relation = RelationMapper.FromEntityId(group.Key).ToString(), Things = new List <GetRelationsResponse.RelationsThings.IdTitle>(), }; foreach (var th in group) { var thingIdTitle = new GetRelationsResponse.RelationsThings.IdTitle { ThingId = ThingIdHelper.Create(th.Thing2_Fqdn, th.Thing2_US) }; var thing2 = this.Find(th.Thing2_Fqdn, th.Thing2_US); if (thing2 != null) { thingIdTitle.Title = thing2.Title; } rt.Things.Add(thingIdTitle); } ret.RelationThings.Add(rt); } return(Ok(ret)); }
public IActionResult QueryMyRoles([FromBody] QueryMyRolesRequest value) { var session = this.GetSession(value.Session, true); T2D.Entities.BaseThing thing = this.Find(value.ThingId) .Include(t => t.ThingRoles) .FirstOrDefault() ; if (thing == null) { return(BadRequest($"Thing '{value.ThingId}' do not exists.")); } //TODO: check that session has right to if (!AttributeSecurity.QueryMyRolesRight(thing, session)) { return(BadRequest($"Not enough priviledges to query roles for thing {value.ThingId}.")); } // explicit loading - select those thingRoleMembers, where ThingRoleMember is one of things in session // have to use Lists List <Guid> sessionThings = new List <Guid> { session.EntryPoint_ThingId }; foreach (var item in session.SessionAccesses) { if (!sessionThings.Contains(item.ThingId)) { sessionThings.Add(item.ThingId); } } List <Guid> thingRoles = new List <Guid>(); thingRoles.AddRange(thing.ThingRoles.Select(tr => tr.Id)); var thingRoleMembers = dbc.ThingRoleMembers .Where(trm => sessionThings.Contains(trm.ThingId) && thingRoles.Contains(trm.ThingRoleId)) .ToList() ; var ret = new QueryMyRolesResponse { Roles = new List <string>(), }; var roleIds = new List <int>(); // add roles and add to SessionAccess foreach (var item in thingRoleMembers) { if (item.ThingRole != null) { int roleId = item.ThingRole.RoleId; if (!roleIds.Contains(roleId)) { roleIds.Add(roleId); } if (!session.SessionAccesses.Any(sa => sa.RoleId == roleId && sa.ThingId == thing.Id)) { dbc.SessionAccesses.Add(new SessionAccess { RoleId = roleId, SessionId = session.Id, ThingId = thing.Id }); } } } dbc.SaveChanges(); ret.Roles.AddRange( dbc.Roles .Where(r => roleIds.Contains(r.Id)) .Select(r => r.Name) .ToList() ); return(Ok(ret)); }