Ejemplo n.º 1
0
        public string GetRoleInvitationForUser(string relationshipAppUserId)
        {
            string roleInvitation;

            using (var session = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
            {
                roleInvitation = GetRoleInvitationFromUserId(relationshipAppUserId, session);
            }

            if (roleInvitation == null
                &&
                !TenantManager.CurrentTenancy.Equals(
                    DefaultRoles.SuperAdmin,
                    StringComparison.OrdinalIgnoreCase))
            {
                using (ContextRegistry.NamedContextsFor(GetType()))
                {
                    using (var session = DocumentStoreLocator.ContextualResolve())
                    {
                        roleInvitation = GetRoleInvitationFromUserId(relationshipAppUserId, session);
                    }
                }
            }

            return(roleInvitation);
        }
Ejemplo n.º 2
0
        public bool SendInvitationEmail(string sendTo, Uri currentUrl, ApplicationUser currentUser, string tenancy)
        {
            using (ContextRegistry.NamedContextsFor(GetType()))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var invitation =
                        session.Query <Invitation>().FirstOrDefault(
                            i => i.SentTo == sendTo && i.Status != InvitationStatus.Deleted);

                    if (invitation == null)
                    {
                        return(false);
                    }

                    if (currentUrl != null)
                    {
                        var emailSender = this.CreateEmailSender(
                            invitation.SentTo, invitation.Tenancy, invitation.AuthorizationCode.UrlEncodedCode, currentUrl);
                        emailSender.Send();
                    }

                    invitation.DateSent = DateTime.UtcNow;
                    invitation.Status   = invitation.ResentTimes > 0 ? InvitationStatus.ReSent : InvitationStatus.Sent;
                    invitation.ResentTimes++;
                    invitation.InvitingUserId = currentUser.PrincipalId;
                    session.SaveChanges();

                    new UserManager().SetStatusUserByPrincipalId(invitation.AcceptingAppUserId);
                    return(true);
                }
            }
        }
        public void DeletePackage(string id)
        {
            using (var cntx = ContextRegistry.NamedContextsFor(this.GetType()))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var query =
                        (from packs in session.Query <Lok.Unik.ModelCommon.Client.ContentPackage>() select packs)
                        .ToArray()
                        .FirstOrDefault(x => x.Id.ToString() == id);

                    session.Delete(query);
                    session.SaveChanges();

                    if (query != null)
                    {
                        var objPath = Path.Combine(query.ConfigurationRelativePath, string.Format("{0}.zip", query.Id));
                        if (File.Exists(objPath))
                        {
                            File.Delete(objPath);
                            _log.InfoFormat("{0} has been deleted and removed from '{1}'", query.Name, objPath);
                        }
                    }
                }
            }
        }
        public IEnumerable <Tag> GetTagsFromContentPackage()
        {
            var tags = new List <Tag>();

            using (var ntx = ContextRegistry.NamedContextsFor(this.GetType()))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var query =
                        (from packages in session.Query <Lok.Unik.ModelCommon.Client.ContentPackage>() select packages).
                        ToArray();

                    //foreach (var contentPackage in query)
                    //{
                    //    tags.AddRange(contentPackage.Tags.Select(tag => new Models.Tag
                    //    {
                    //        Id = tag.Id,
                    //        Name = tag.Attribute,
                    //        Type = tag.Type.ToString(),
                    //        Category = tag.Category.Name,
                    //        CreateDate = tag.CreateDate,
                    //        Color = tag.Category.Color.ToString()
                    //    }));
                    //}

                    return(tags);
                }
            }
        }
Ejemplo n.º 5
0
        public static void CreateIndexes()
        {
            using (var nctxs = ContextRegistry.NamedContextsFor(typeof(DeviceManager)))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var config = Catalog.Factory.Resolve <IConfig>();
                    var dbName = config[CommonConfiguration.DefaultDataDatabase];

                    var tenancyUris = ContextRegistry.ContextsOf("Tenancy");
                    if (tenancyUris.Any())
                    {
                        var tenancyUri = tenancyUris.First();
                        var tenancy    = tenancyUri.Segments.Count() > 1 ? tenancyUri.Segments[1] : string.Empty;

                        if (!string.IsNullOrEmpty(tenancy)
                            &&
                            !tenancy.Equals(Tenants.SystemOwner, StringComparison.InvariantCultureIgnoreCase))
                        {
                            dbName = tenancy;
                        }
                    }

                    var store = session.Advanced.DocumentStore;
                    IndexesManager.CreateIndexes(store, dbName, typeof(AllDeviceIndex));
                }
            }
        }
Ejemplo n.º 6
0
        public IDevice SaveNewDevice(IDevice deviceDb)
        {
            using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var objId = Guid.NewGuid();
                    deviceDb.Id = objId;

                    deviceDb.Tags.Add(new Tag
                    {
                        Id         = Guid.NewGuid(),
                        Type       = TagType.Device,
                        Attribute  = deviceDb.Name,
                        Value      = deviceDb.Name,
                        CreateDate = DateTime.UtcNow,
                        Category   = new TagCategory
                        {
                            Name  = objId.ToString(),
                            Color = CategoryColor.Default
                        }
                    });

                    session.Store(deviceDb);
                    session.SaveChanges();

                    return(deviceDb);
                }
            }
        }
        public void DeleteLibrary(string id)
        {
            using (var cntx = ContextRegistry.NamedContextsFor(this.GetType()))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var libs = from q in session.Query <Lok.Unik.ModelCommon.Client.ContentLibrary>() select q;

                    var lib = libs.ToArray().FirstOrDefault(x => x.Id.ToString() == id);

                    if (lib != null)
                    {
                        foreach (var item in lib.Files)
                        {
                            var relativePath = Path.Combine(lib.BasePath, string.Format("{0}.zip", item.Id));
                            if (File.Exists(relativePath))
                            {
                                File.Delete(relativePath);
                            }
                            _log.InfoFormat("Deleting {0} stored at {1}", lib.Title, lib.BasePath);
                        }
                    }

                    session.Delete(lib);
                    if (lib != null)
                    {
                        _log.InfoFormat("{0} Library has been deleted successfully", lib.Title);
                    }
                    session.SaveChanges();
                }
            }
        }
Ejemplo n.º 8
0
        public IEnumerable <Tag> GetTagsByCategoryName(EntityType entity, string categoryName)
        {
            var tags = new List <Tag>();

            using (ContextRegistry.NamedContextsFor(this.GetType()))
            {
                using (var session = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
                {
                    switch (entity)
                    {
                    case EntityType.User:
                    {
                        var query = session.Query <User>().ToList();
                        tags.AddRange(query.SelectMany(z => z.Tags.Where(t => t.Category.Name == categoryName)).Distinct().ToList());
                    }

                    break;

                    case EntityType.ItemRegistration:
                    {
                        var query = session.Query <ItemRegistration>().ToList();
                        tags.AddRange(query.SelectMany(z => z.Tags.Where(t => t.Category.Name == categoryName)).Distinct().ToList());
                    }

                    break;
                    }
                }
            }

            return(tags);
        }
Ejemplo n.º 9
0
        private static void DeviceHealthStatusInitialize(Kiosk dev)
        {
            using (
                var ctx =
                    ContextRegistry.NamedContextsFor(ContextRegistry.CreateNamed(ContextRegistry.Kind,
                                                                                 UnikContextTypes.
                                                                                 UnikWarehouseContextResourceKind)))
            {
                using (var dc = DocumentStoreLocator.ContextualResolve())
                {
                    var deviceHealthChangeEvt = new DeviceHealthStatusEvent
                    {
                        DeviceId    = dev.Id,
                        DeviceName  = dev.Name,
                        From        = HealthStatus.Unknown,
                        To          = HealthStatus.Green,
                        DeviceTags  = dev.Tags,
                        TimeChanged = DateTime.UtcNow
                    };

                    dc.Store(deviceHealthChangeEvt);
                    dc.SaveChanges();
                }
            }
        }
        public void DeleteItemPackage(string id)
        {
            using (var cntx = ContextRegistry.NamedContextsFor(this.GetType()))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var libTarget =
                        (from libs in session.Query <Lok.Unik.ModelCommon.Client.ContentLibrary>()
                         where libs.Files.Any(file => file.Id == Guid.Parse(id))
                         select libs)
                        .FirstOrDefault();

                    if (libTarget != null)
                    {
                        var tempItem = libTarget.Files.FirstOrDefault(file => file.Id == Guid.Parse(id));

                        if (tempItem != null)
                        {
                            var objPath = Path.Combine(libTarget.BasePath, string.Format("{0}.zip", tempItem.Id));
                            if (File.Exists(objPath))
                            {
                                File.Delete(objPath);
                                _log.InfoFormat("'{0}' LibraryItem has been removed from '{1}'", tempItem.Title, objPath);
                            }

                            libTarget.Files.Remove(tempItem);
                        }
                    }

                    session.SaveChanges();
                }
            }
        }
        public void DeleteUserRole(string id)
        {
            using (var cntx = ContextRegistry.NamedContextsFor(this.GetType()))
            {
                using (var session = DocumentStoreLocator.ContextualResolve())
                {
                    var query =
                        (from libs in session.Query <Lok.Unik.ModelCommon.Client.ContentLibrary>() select libs)
                        .ToArray();

                    var temp = query;

                    for (var i = 0; i < temp.Length; i++)
                    {
                        for (var j = 0; j < temp[i].InvitedUsers.Count; j++)
                        {
                            var users = new UserManager().GetAll();

                            foreach (var user in users)
                            {
                                if (user.Id.ToString().Equals(id))
                                {
                                    query[i].InvitedUsers.RemoveAt(j);
                                }
                            }
                        }
                    }

                    session.SaveChanges();
                }
            }
        }
Ejemplo n.º 12
0
        public static IEnumerable <T> GetFromRepository <T>(QuerySpecification querySpecification = null) where T : class, new()
        {
            using (var ctx = ContextRegistry.NamedContextsFor(
                       ContextRegistry.CreateNamed(ContextRegistry.Kind,
                                                   UnikContextTypes.UnikWarehouseContextResourceKind)))
            {
                List <T> reports    = new List <T>();
                var      repository = ReportDataProvider.OpenCoreRepository <T>();

                Func <QuerySpecification, DataEnvelope <T, NoMetadata> > function = null;
                if (querySpecification == null)
                {
                    function = qe => repository.All();
                }
                else
                {
                    function = qe => repository.Query(qe);
                }

                bool loop = true;
                while (loop)
                {
                    var qr = function(querySpecification);
                    if (qr.Items.Any())
                    {
                        reports.AddRange(qr.Items.ToArray());
                    }
                    qr.Bookmark.Forward();
                    loop = qr.Bookmark.More;
                }

                return(reports);
            }
        }
Ejemplo n.º 13
0
        public bool UserHasInvitation(string contactEmail, string tenancy, out string code)
        {
            bool existInvitation;

            using (var session = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
            {
                existInvitation = ExistValidInvitation(contactEmail, tenancy, session, out code);
            }

            if (!existInvitation)
            {
                if (!DefaultRoles.SuperAdmin.Equals(tenancy, StringComparison.InvariantCultureIgnoreCase))
                {
                    using (ContextRegistry.NamedContextsFor(GetType()))
                    {
                        using (var session1 = DocumentStoreLocator.ContextualResolve())
                        {
                            existInvitation = ExistValidInvitation(contactEmail, tenancy, session1, out code);
                        }
                    }
                }
            }

            return(existInvitation);
        }
Ejemplo n.º 14
0
        public void ChangeRoleUser(Guid userId, string newRoleToChange, string userEmail, ApplicationUser changingUser)
        {
            var aUser = GetCompleteUserQuery(userId);

            using (var session = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
            {
                var newRole = session.Load <ApplicationRole>(newRoleToChange);

                if (changingUser.Tenancy.Equals(DefaultRoles.SuperAdmin, StringComparison.OrdinalIgnoreCase))
                {
                    //    //no send invitation..
                    var invitation = new InvitationManager().GetInvitationByEmail(userEmail);

                    if (invitation != null)
                    {
                        var current = session.Load <Invitation>(invitation.Id);
                        current.Role = newRole.Name;
                        session.SaveChanges();
                    }
                }
                else
                {
                    //tenant
                    var invitation = new InvitationManager().GetInvitationByEmail(aUser.AppUser.ContactEmail);

                    if (invitation != null)
                    {
                        using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
                        {
                            using (var sessionInv = DocumentStoreLocator.ContextualResolve())
                            {
                                var current = sessionInv.Load <Invitation>(invitation.Id);
                                current.Role = newRole.Name;
                                sessionInv.SaveChanges();
                            }
                        }
                    }
                }

                if (aUser.AppUser == null)
                {
                    return;
                }
                if (aUser.AppUser.AccountRoles == null)
                {
                    return;
                }


                var currentUser = session.Load <ApplicationUser>(aUser.AppUser.Id);

                if (newRole != null && !currentUser.AccountRoles.Contains(newRole.Id))
                {
                    //Changing current role by the new one
                    currentUser.AccountRoles.Clear();
                    currentUser.AccountRoles.Add(newRole.Id);
                }
                session.SaveChanges();
            }
        }
Ejemplo n.º 15
0
 public static void StoreInRepository <T>(T document) where T : class, new()
 {
     using (var ctx = ContextRegistry.NamedContextsFor(
                ContextRegistry.CreateNamed(ContextRegistry.Kind,
                                            UnikContextTypes.UnikWarehouseContextResourceKind)))
     {
         var repository = ReportDataProvider.OpenCoreRepository <T>();
         repository.Store(document);
     }
 }
Ejemplo n.º 16
0
 public void AssignTags <T>(Guid id, IList <Tag> tagsList, bool clear) where T : ITaggableEntity
 {
     using (ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             AssignTagsGeneric <T>(id, tagsList, clear, session);
         }
     }
 }
Ejemplo n.º 17
0
 private IDocumentSession ReturnContext(string tenancy)
 {
     using (var ctx = ContextRegistry.NamedContextsFor(GetType()))
     {
         return(string.IsNullOrEmpty(tenancy) ||
                DefaultRoles.SuperAdmin.Equals(tenancy, StringComparison.InvariantCultureIgnoreCase)
                    ? DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute)
                    : DocumentStoreLocator.ContextualResolve());
     }
 }
Ejemplo n.º 18
0
 public IDocumentSession Sessiones()
 {
     using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             return(session);
         }
     }
 }
Ejemplo n.º 19
0
 public static void DeleteBatchInRepository <T>(List <T> list) where T : class, new()
 {
     using (var ctx = ContextRegistry.NamedContextsFor(
                ContextRegistry.CreateNamed(ContextRegistry.Kind,
                                            UnikContextTypes.UnikWarehouseContextResourceKind)))
     {
         var repository = ReportDataProvider.OpenCoreRepository <T>();
         repository.DeleteBatch(list);
     }
 }
Ejemplo n.º 20
0
 public IEnumerable <T> GetDevicesByType <T>() where T : Device
 {
     using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             return((from entity in session.Query <T>() select entity).ToArray());
         }
     }
 }
Ejemplo n.º 21
0
 public IEnumerable <Application> GetAllAplications()
 {
     using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             var list = session.Query <Application>().ToArray();
             return(list);
         }
     }
 }
Ejemplo n.º 22
0
 public IEnumerable <Lok.Unik.ModelCommon.Client.SchedulePlan> GetAll()
 {
     using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             var q2 = from schedule in session.Query <Lok.Unik.ModelCommon.Client.SchedulePlan>() select schedule;
             return(q2.ToArray());
         }
     }
 }
Ejemplo n.º 23
0
 public void RemoveTags <T>(Guid id) where T : ITaggableEntity
 {
     using (ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             ResolveEntity <T>(id, session);
             session.SaveChanges();
         }
     }
 }
Ejemplo n.º 24
0
 public IEnumerable <GroupLink> GetAllGroupLink()
 {
     using (ContextRegistry.NamedContextsFor(GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             var q2 = from groupLink in session.Query <GroupLink>() select groupLink;
             return(q2.ToArray());
         }
     }
 }
Ejemplo n.º 25
0
 public void Store <T>(T device) where T : Device
 {
     using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             session.Store(device);
             session.SaveChanges();
         }
     }
 }
Ejemplo n.º 26
0
 public IEnumerable <Device> GetDevices()
 {
     using (var nctxs = ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             var q = from entity in session.Query <Device, AllDeviceIndex>() select entity;
             return(q.ToArray());
         }
     }
 }
Ejemplo n.º 27
0
 public IEnumerable <GroupLink> GetAllGroupLinkWithGroupTwoType(TagType tagType)
 {
     using (ContextRegistry.NamedContextsFor(GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             var q2 = session.Query <GroupLink>().Where(x => x.GroupTwo.Type == tagType).ToList();
             return(q2.ToArray());
         }
     }
 }
 public IEnumerable <Lok.Unik.ModelCommon.Client.ContentPackage> GetAll()
 {
     using (var cntx = ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             var query = from cp in session.Query <Lok.Unik.ModelCommon.Client.ContentPackage>() select cp;
             return(query.ToArray());
         }
     }
 }
Ejemplo n.º 29
0
        public void Remove(Guid userId, ApplicationUser currentUser)
        {
            var aUser = GetCompleteUserQuery(userId);

            if (currentUser.Tenancy.Equals(DefaultRoles.SuperAdmin, StringComparison.OrdinalIgnoreCase))
            {
                using (var session = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
                {
                    if (!string.IsNullOrEmpty(aUser.InvitationId))
                    {
                        var invitation = session.Load <Invitation>(aUser.InvitationId);
                        invitation.Status = InvitationStatus.Deleted;
                        session.SaveChanges();
                    }

                    else
                    {
                        //    //no send invitation..
                        var invitation = new InvitationManager().GetInvitationByEmail(aUser.AppUser.ContactEmail);

                        if (invitation != null)
                        {
                            var current = session.Load <Invitation>(invitation.Id);
                            current.Status = InvitationStatus.Deleted;
                            session.SaveChanges();
                        }
                    }
                }
            }
            else
            {
                using (ContextRegistry.NamedContextsFor(GetType()))
                {
                    using (var sessionInv = DocumentStoreLocator.ContextualResolve())
                    {
                        if (!string.IsNullOrEmpty(aUser.InvitationId))
                        {
                            var invitation = sessionInv.Load <Invitation>(aUser.InvitationId);
                            invitation.Status = InvitationStatus.Deleted;
                            sessionInv.SaveChanges();
                        }
                    }
                }
            }
            using (var sessionUser = DocumentStoreLocator.ResolveOrRoot(CommonConfiguration.CoreDatabaseRoute))
            {
                aUser.AppUser.Status       = UserStatus.Deleted;
                aUser.AppUser.AccountRoles = null;
                var current = sessionUser.Load <ApplicationUser>(aUser.AppUser.Id);
                current.Status = aUser.AppUser.Status;
                sessionUser.SaveChanges();
            }
        }
Ejemplo n.º 30
0
 public IEnumerable <Tag> GetAllTags()
 {
     using (ContextRegistry.NamedContextsFor(this.GetType()))
     {
         using (var session = DocumentStoreLocator.ContextualResolve())
         {
             var q    = from tag in session.Query <Tag>() select tag;
             var list = q.ToList();
             return(list);
         }
     }
 }