Пример #1
0
        private static void AddConcept(XmlFile documentDB, XElement item, CompAgriConnection ctx)
        {
            Term currentTerm = null;

            foreach (XElement child in item.Descendants())
            {
                if (child.Name == "DESCRIPTOR")
                {
                    currentTerm = AddTerm(child, documentDB, ctx);
                }
                else if (child.Name == "BT")
                {
                    AddParentRelationToTerm(currentTerm, child, documentDB, ctx);
                }
                else if (child.Name == "NT")
                {
                    AddChildRelationToTerm(currentTerm, child, documentDB, ctx);
                }
                else
                {
                    AddPropertyToTerm(ctx, currentTerm, child);
                }
            }
            ctx.SaveChanges();
        }
Пример #2
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            var skipAuthorization = actionContext.ActionDescriptor.GetFilters().OfType <AllowAnonymousAttribute>().Count() != 0;

            skipAuthorization = skipAuthorization || actionContext.ActionDescriptor.ControllerDescriptor.GetFilters().OfType <AllowAnonymousAttribute>().Count() != 0;

            if (skipAuthorization)
            {
                return(true);
            }

            var token = UserUtils.GetUserToken(actionContext.Request);

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

            using (var db = new CompAgriConnection())
            {
                var user = db.User.FirstOrDefault(u => u.Token == token);
                if (user == null)
                {
                    return(false);
                }

                return(UserHasPermissionToAction(user, actionContext));
            }
        }
Пример #3
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            var skipAuthorization = actionContext.ActionDescriptor.GetFilters().OfType<AllowAnonymousAttribute>().Count() != 0;
            skipAuthorization = skipAuthorization || actionContext.ActionDescriptor.ControllerDescriptor.GetFilters().OfType<AllowAnonymousAttribute>().Count() != 0;

            if (skipAuthorization)
            {
                return true;
            }

            var token = UserUtils.GetUserToken(actionContext.Request);

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

            using (var db = new CompAgriConnection())
            {
                var user = db.User.FirstOrDefault(u => u.Token == token);
                if (user == null)
                {
                    return false;
                }

                return UserHasPermissionToAction(user, actionContext);
            }
        }
Пример #4
0
        internal static void MoveNode(int nodeId, int? oldParentId, int? newParentId)
        {
            using (CompAgriConnection ctx = new CompAgriConnection())
            {
                var relation = ctx.Relations.Where(o => o.Relation_Parent_Term_Id == oldParentId && o.Relation_Child_Term_Id == nodeId)
                    .FirstOrDefault();

                if (relation != null)
                {
                    relation.Relation_Parent_Term_Id = newParentId;
                }
                else
                {
                    relation = new Relation
                    {
                        Relation_Child_Term_Id = nodeId,
                        Relation_Parent_Term_Id = newParentId
                    };

                    ctx.Relations.Add(relation);
                }

                ctx.SaveChanges();
            }
        }
Пример #5
0
        internal static int AddNode(int xmlFileId, String name, int parentId)
        {
            try
            {
                using (CompAgriConnection ctx = new CompAgriConnection())
                {
                    var newTerm = new Term()
                    {
                        Term_XmlFile_Id = xmlFileId,
                        Term_Title      = name
                    };

                    newTerm.Relation1.Add(new Relation {
                        Relation_Parent_Term_Id = parentId, Relation_Child_Term_Id = newTerm.Term_Id
                    });

                    ctx.Terms.Add(newTerm);
                    ctx.SaveChanges();
                    return(newTerm.Term_Id);
                }
            }
            catch (Exception)
            {
                // Do Nothing !
            }
            return(-1);
        }
Пример #6
0
        internal static void MoveNode(int nodeId, int?oldParentId, int?newParentId)
        {
            using (CompAgriConnection ctx = new CompAgriConnection())
            {
                var relation = ctx.Relations.Where(o => o.Relation_Parent_Term_Id == oldParentId && o.Relation_Child_Term_Id == nodeId)
                               .FirstOrDefault();

                if (relation != null)
                {
                    relation.Relation_Parent_Term_Id = newParentId;
                }
                else
                {
                    relation = new Relation
                    {
                        Relation_Child_Term_Id  = nodeId,
                        Relation_Parent_Term_Id = newParentId
                    };

                    ctx.Relations.Add(relation);
                }

                ctx.SaveChanges();
            }
        }
Пример #7
0
 public void Delete(int id)
 {
     using (var db = new CompAgriConnection())
     {
         var user = db.User.FirstOrDefault(u => u.User_Id == id);
         db.User.Remove(user);
         db.SaveChanges();
     }
 }
Пример #8
0
        private static void AddPropertyToTerm(CompAgriConnection ctx, Term currentTerm, XElement child)
        {
            Property newProperty = new Property()
            {
                Property_Key     = child.Name.LocalName,
                Property_Value   = child.Value,
                Property_Term_Id = currentTerm.Term_Id
            };

            currentTerm.Property.Add(newProperty);
            ctx.Properties.Add(newProperty);
        }
Пример #9
0
        public UserDto Get(int id)
        {
            using (var db = new CompAgriConnection())
            {
                var user = db.User.FirstOrDefault(u => u.User_Id == id);

                if (user == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                return new UserDto(user);
            }
        }
Пример #10
0
        internal static void DeleteNode(int nodeId, int parentId)
        {
            using (CompAgriConnection ctx = new CompAgriConnection())
            {
                var relationToDelete = ctx.Relations.Where(o => o.Relation_Parent_Term_Id == parentId && o.Relation_Child_Term_Id == nodeId)
                                       .FirstOrDefault();

                if (relationToDelete != null)
                {
                    ctx.Relations.Remove(relationToDelete);
                    ctx.SaveChanges();
                }
            }
        }
Пример #11
0
        public static User GetUser(HttpRequestMessage request)
        {
            var token = GetUserToken(request);

            if (token == null)
            {
                return null;
            }

            using (var db = new CompAgriConnection())
            {
                return db.User.FirstOrDefault(u => u.Token == token);
            }
        }
Пример #12
0
        private static Term AddTerm(XElement description, XmlFile documentDB, CompAgriConnection ctx)
        {
            Term currentTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == description.Value);

            if (currentTerm == null)
            {
                currentTerm                 = new Term();
                currentTerm.Term_Title      = description.Value;
                currentTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                ctx.Terms.Add(currentTerm);
                ctx.SaveChanges();
            }
            return(currentTerm);
        }
Пример #13
0
        internal static void DeleteNode(int nodeId, int parentId)
        {
            using (CompAgriConnection ctx = new CompAgriConnection())
            {
                var relationToDelete = ctx.Relations.Where(o => o.Relation_Parent_Term_Id == parentId && o.Relation_Child_Term_Id == nodeId)
                    .FirstOrDefault();

                if (relationToDelete != null)
                {
                    ctx.Relations.Remove(relationToDelete);
                    ctx.SaveChanges();
                }
            }
        }
Пример #14
0
 public IEnumerable<UserDto> Get()
 {
     using (var db = new CompAgriConnection())
     {
         return db.User.Select(u => new UserDto()
         {
             User_Id = u.User_Id,
             UserName = u.UserName,
             Name = u.Name,
             LastName = u.LastName,
             Email = u.Email,
             UserProfile_Id = u.UserProfile_Id,
         }).ToList();
     }
 }
Пример #15
0
        public UserDto Get()
        {
            string token = UserUtils.GetUserToken(Request);

            if (token == null)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            using (var db = new CompAgriConnection())
            {
                var user = db.User.FirstOrDefault(u => u.Token == token);
                if (user == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return new UserDto(user, true);
            }
        }
Пример #16
0
        internal static void MoveNode(int nodeId, int oldParentId, int newParentId)
        {
            try
            {
                using (CompAgriConnection ctx = new CompAgriConnection())
                {
                    var relation = ctx.Relations.Where(o => o.Relation_Parent_Term_Id == oldParentId && o.Relation_Child_Term_Id == nodeId)
                                   .FirstOrDefault();

                    if (relation != null)
                    {
                        relation.Relation_Parent_Term_Id = newParentId;
                        ctx.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #17
0
        public void Delete()
        {
            string token = UserUtils.GetUserToken(Request);

            if (token == null)
            {
                return;
            }

            using (var db = new CompAgriConnection())
            {
                var user = db.User.FirstOrDefault(u => u.Token == token);
                if (user == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                user.Token = null;
                db.SaveChanges();
            }
        }
Пример #18
0
        public UserDto Post(UserDto user)
        {
            User userFromDatabase;
            using (var db = new CompAgriConnection())
            {
                if (user.Email != null)
                {
                    userFromDatabase = db.User.FirstOrDefault(u => u.Email == user.Email);
                }
                else if (user.UserName != null)
                {
                    userFromDatabase = db.User.FirstOrDefault(u => u.UserName == user.UserName);
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                if (userFromDatabase == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                var hashedPassword = PasswordUtils.HashPassword(user.Password, userFromDatabase.PasswordSalt);

                if (hashedPassword != userFromDatabase.Password)
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else
                {
                    userFromDatabase.Token = TokenUtils.GenerateToken();
                    db.SaveChanges();

                    var userToSend = new UserDto(userFromDatabase, true);
                    return userToSend;
                }
            }
        }
Пример #19
0
        private bool UserHasPermissionToAction(User user, HttpActionContext actionContext)
        {
            var controllerName = actionContext.ControllerContext.ControllerDescriptor.ControllerName;
            var actionName     = actionContext.ActionDescriptor.ActionName;

            using (var db = new CompAgriConnection())
            {
                var permissions = db.UserProfilePermission.Where(up => up.UserProfile_Id == user.UserProfile_Id && up.ControllerName == controllerName && up.ActionName == actionName);

                if (permissions.Count() == 0)
                {
                    return(true);
                }
                else if (permissions.FirstOrDefault(up => up.Deny == true) != null)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Пример #20
0
        private bool UserHasPermissionToAction(User user, HttpActionContext actionContext)
        {
            var controllerName = actionContext.ControllerContext.ControllerDescriptor.ControllerName;
            var actionName = actionContext.ActionDescriptor.ActionName;

            using (var db = new CompAgriConnection())
            {
                var permissions = db.UserProfilePermission.Where(up => up.UserProfile_Id == user.UserProfile_Id && up.ControllerName == controllerName && up.ActionName == actionName);

                if (permissions.Count() == 0)
                {
                    return true;
                }
                else if (permissions.FirstOrDefault(up => up.Deny == true) != null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
Пример #21
0
        public UserDto Post([FromBody] UserDto userDto)
        {
            if (!userDto.IsValid())
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            using (var db = new CompAgriConnection())
            {
                var previousUser = db.User.FirstOrDefault(u => (u.UserName != null && u.UserName == userDto.UserName) || (u.Email != null && u.Email == userDto.Email));
                if (previousUser != null)
                {
                    throw WebExceptionsFactory.GetUserDuplicatedException();
                }

                var userBeforeSave = userDto.User();
                userBeforeSave.PasswordSalt = PasswordUtils.GenerateSalt();
                userBeforeSave.Password = PasswordUtils.HashPassword(userBeforeSave.Password, userBeforeSave.PasswordSalt);

                var user = db.User.Add(userBeforeSave);
                db.SaveChanges();
                return new UserDto(user);
            }
        }
Пример #22
0
        internal static void UploadFile(string filePath)
        {
            using (CompAgriConnection ctx = new CompAgriConnection())
            {
                ctx.Configuration.AutoDetectChangesEnabled = false;

                string    path     = filePath;
                string    fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1);
                XDocument docX     = XDocument.Load(path);

                XmlFile documentDB = new XmlFile();
                ctx.XmlFiles.Add(documentDB);
                documentDB.XmlFile_Name = fileName;

                ctx.SaveChanges();

                IEnumerable <XElement> concept = docX.Descendants("CONCEPT");
                foreach (XElement item in concept)
                {
                    AddConcept(documentDB, item, ctx);
                }
                ctx.SaveChanges();
            }
        }
Пример #23
0
        private static void AddChildRelationToTerm(Term currentTerm, XElement child, XmlFile documentDB, CompAgriConnection ctx)
        {
            Term childTerm = null;

            childTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == child.Value);
            if (childTerm == null)
            {
                childTerm                 = new Term();
                childTerm.Term_Title      = child.Value;
                childTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                ctx.Terms.Add(childTerm);
                ctx.SaveChanges();
            }

            Relation relation = new Relation()
            {
                Term  = currentTerm,
                Term1 = childTerm
            };

            ctx.Relations.Add(relation);
        }
Пример #24
0
        private static void AddParentRelationToTerm(Term currentTerm, XElement parentInfo, XmlFile documentDB, CompAgriConnection ctx)
        {
            Term parentTerm = null;

            parentTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == parentInfo.Value);
            if (parentTerm == null)
            {
                parentTerm                 = new Term();
                parentTerm.Term_Title      = parentInfo.Value;
                parentTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                ctx.Terms.Add(parentTerm);
                ctx.SaveChanges();
            }

            Relation relation = new Relation()
            {
                Term  = parentTerm,
                Term1 = currentTerm
            };

            ctx.Relations.Add(relation);
        }
Пример #25
0
        private static void AddParentRelationToTerm(Term currentTerm, XElement parentInfo, XmlFile documentDB, CompAgriConnection ctx)
        {
            Term parentTerm = null;
            parentTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == parentInfo.Value);
            if (parentTerm == null)
            {
                parentTerm = new Term();
                parentTerm.Term_Title = parentInfo.Value;
                parentTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                ctx.Terms.Add(parentTerm);
                ctx.SaveChanges();
            }

            Relation relation = new Relation()
            {
                Term = parentTerm,
                Term1 = currentTerm
            };
            ctx.Relations.Add(relation);
        }
Пример #26
0
        internal static void UploadFile(string filePath)
        {
            using (CompAgriConnection ctx = new CompAgriConnection())
            {
                ctx.Configuration.AutoDetectChangesEnabled = false;

                string path = filePath;
                string fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1);
                XDocument docX = XDocument.Load(path);

                XmlFile documentDB = new XmlFile();
                ctx.XmlFiles.Add(documentDB);
                documentDB.XmlFile_Name = fileName;

                ctx.SaveChanges();

                IEnumerable<XElement> concept = docX.Descendants("CONCEPT");
                foreach (XElement item in concept)
                {
                    AddConcept(documentDB, item, ctx);
                }
                ctx.SaveChanges();

            }
        }
Пример #27
0
        internal static void UploadFile(string filePath)
        {
            try
            {
                using (CompAgriConnection ctx = new CompAgriConnection())
                {
                    ctx.Configuration.AutoDetectChangesEnabled = false;


                    var path = "";
                    path = filePath;
                    string    fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1);
                    XDocument docX     = XDocument.Load(path);

                    try
                    {
                        XmlFile documentDB = new XmlFile();
                        ctx.XmlFiles.Add(documentDB);
                        documentDB.XmlFile_Name = fileName;

                        ctx.SaveChanges();

                        IEnumerable <XElement> concept = docX.Descendants("CONCEPT");
                        int countConcept = 0;
                        foreach (XElement item in concept)
                        {
                            Term currentTerm = null;
                            foreach (XElement child in item.Descendants())
                            {
                                if (child.Name == "DESCRIPTOR")
                                {
                                    currentTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == child.Value);
                                    if (currentTerm == null)
                                    {
                                        currentTerm                 = new Term();
                                        currentTerm.Term_Title      = child.Value;
                                        currentTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                                        ctx.Terms.Add(currentTerm);
                                        ctx.SaveChanges();
                                    }
                                }
                                else if (child.Name == "BT")
                                {
                                    Term parentTerm = null;
                                    parentTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == child.Value);
                                    if (parentTerm == null)
                                    {
                                        parentTerm                 = new Term();
                                        parentTerm.Term_Title      = child.Value;
                                        parentTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                                        ctx.Terms.Add(parentTerm);
                                        ctx.SaveChanges();
                                    }

                                    Relation relation = new Relation()
                                    {
                                        Term  = parentTerm,
                                        Term1 = currentTerm
                                    };

                                    ctx.Relations.Add(relation);
                                }
                                else if (child.Name == "NT")
                                {
                                    Term childTerm = null;
                                    childTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == child.Value);
                                    if (childTerm == null)
                                    {
                                        childTerm                 = new Term();
                                        childTerm.Term_Title      = child.Value;
                                        childTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                                        ctx.Terms.Add(childTerm);
                                        ctx.SaveChanges();
                                    }

                                    Relation relation = new Relation()
                                    {
                                        Term  = currentTerm,
                                        Term1 = childTerm
                                    };

                                    ctx.Relations.Add(relation);
                                }
                                else
                                {
                                    Property newProperty = new Property()
                                    {
                                        Property_Key     = child.Name.LocalName,
                                        Property_Value   = child.Value,
                                        Property_Term_Id = currentTerm.Term_Id
                                    };

                                    currentTerm.Property.Add(newProperty);

                                    ctx.Properties.Add(newProperty);
                                }
                            }
                            countConcept++;
                            if (countConcept % 1 == 0)
                            {
                                ctx.SaveChanges();
                            }
                        } // End of For Each !!!
                        ctx.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        String str  = e.ToString();
                        String str2 = e.Message;
                        throw e;
                    }
                }
            }
            catch (Exception e)
            {
                String str  = e.ToString();
                String str2 = e.Message;
                throw e;
            }
        }
Пример #28
0
        private static void AddPropertyToTerm(CompAgriConnection ctx, Term currentTerm, XElement child)
        {
            Property newProperty = new Property()
            {
                Property_Key = child.Name.LocalName,
                Property_Value = child.Value,
                Property_Term_Id = currentTerm.Term_Id
            };

            currentTerm.Property.Add(newProperty);
            ctx.Properties.Add(newProperty);
        }
Пример #29
0
 private static Term AddTerm(XElement description, XmlFile documentDB, CompAgriConnection ctx)
 {
     Term currentTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == description.Value);
     if (currentTerm == null)
     {
         currentTerm = new Term();
         currentTerm.Term_Title = description.Value;
         currentTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
         ctx.Terms.Add(currentTerm);
         ctx.SaveChanges();
     }
     return currentTerm;
 }
Пример #30
0
        private static void AddChildRelationToTerm(Term currentTerm, XElement child, XmlFile documentDB, CompAgriConnection ctx)
        {
            Term childTerm = null;
            childTerm = documentDB.Term.FirstOrDefault(d => d.Term_Title == child.Value);
            if (childTerm == null)
            {
                childTerm = new Term();
                childTerm.Term_Title = child.Value;
                childTerm.Term_XmlFile_Id = documentDB.XmlFile_Id;
                ctx.Terms.Add(childTerm);
                ctx.SaveChanges();
            }

            Relation relation = new Relation()
            {
                Term = currentTerm,
                Term1 = childTerm
            };
            ctx.Relations.Add(relation);
        }
Пример #31
0
        private static void AddConcept(XmlFile documentDB, XElement item, CompAgriConnection ctx)
        {
            Term currentTerm = null;
            foreach (XElement child in item.Descendants())
            {

                if (child.Name == "DESCRIPTOR")
                {
                    currentTerm = AddTerm(child, documentDB, ctx);
                }
                else if (child.Name == "BT")
                {
                    AddParentRelationToTerm(currentTerm, child, documentDB, ctx);
                }
                else if (child.Name == "NT")
                {
                    AddChildRelationToTerm(currentTerm, child, documentDB, ctx);
                }
                else
                {
                    AddPropertyToTerm(ctx, currentTerm, child);
                }
            }
            ctx.SaveChanges();
        }