Exemplo n.º 1
0
        public IHttpActionResult UpdateAccessPermissionTbl(UserDataModel userDataModal)
        {
            try
            {
                int role   = userDataModal.role;
                int userId = userDataModal.UserId;

                var result = (from a in db.AccessPermissionTbls
                              where a.UserId.Equals(userId)
                              select new
                {
                    a.AccessId,
                    a.PermissionId,
                    a.UserId
                }).ToList();

                var result1 = (from a in db.RoleAccessPermissionTbls
                               where a.RoleId.Equals(role)
                               select new
                {
                    a.RoleId,
                    a.AccessId,
                    a.PermissionId
                }).ToList();

                if (result.Count() > 0)
                {
                    if (result1.Count() > 0)
                    {
                        for (int j = 0; j < result1.Count(); j++)
                        {
                            for (int i = 0; i < result.Count(); i++)
                            {
                                if (result.ToList()[i].AccessId == result1.ToList()[j].AccessId && result.ToList()[i].PermissionId == result1.ToList()[j].PermissionId)
                                {
                                    AccessPermissionTbl accessPermissiontbl = new AccessPermissionTbl();
                                    int userid1    = userId;
                                    int access     = result1.ToList()[j].AccessId;
                                    int permission = result1.ToList()[j].PermissionId;
                                    AccessPermissionTbl accessPermissionTbl1 = db.AccessPermissionTbls.Find(userid1, access, permission);
                                    db.AccessPermissionTbls.Remove(accessPermissionTbl1);
                                    db.SaveChanges();
                                    return(Ok("RoleId, AccessId and PermissionId these combination is already there " +
                                              "and click save again if you have given morethen one Accessname and Permission name..."));
                                }
                            }
                        }
                    }
                }

                if (result1.Count() > 0)
                {
                    for (int i = 0; i < result1.Count(); i++)
                    {
                        AccessPermissionTbl accessPermissionTbl = new AccessPermissionTbl();
                        accessPermissionTbl.AccessId     = result1.ToList()[i].AccessId;
                        accessPermissionTbl.PermissionId = result1.ToList()[i].PermissionId;
                        accessPermissionTbl.UserId       = userId;
                        db.AccessPermissionTbls.Add(accessPermissionTbl);
                    }
                    db.SaveChanges();
                }

                else
                {
                    return(Content(HttpStatusCode.NotFound, "RoleId & UserId is not there"));
                }
                var result10 = (from a in db.AccessPermissionTbls
                                where a.UserId == userId
                                select new { a.AccessId, a.PermissionId }).ToList();

                var result2 = (from b in db.AccessPermissionTbls
                               where b.UserId.Equals(userId)
                               select new
                {
                    b.UserId,
                    b.AccessId,
                    b.PermissionId
                }).ToList();

                if (result1.Count() > 0)
                {
                    for (int i = 0; i < result1.Count(); i++)
                    {
                        RoleAccessPermissionTbl roleAccessPermissionTbl = new RoleAccessPermissionTbl();
                        int roleid       = result1.ToList()[i].RoleId;
                        int accessid     = result1.ToList()[i].AccessId;
                        int permissionid = result1.ToList()[i].PermissionId;
                        RoleAccessPermissionTbl roleAccessPermissionTbl1 = db.RoleAccessPermissionTbls.Find(roleid, accessid, permissionid);
                        db.RoleAccessPermissionTbls.Remove(roleAccessPermissionTbl1);
                    }
                    db.SaveChanges();
                }

                if (result2.Count() > 0)
                {
                    for (int i = 0; i < result2.Count(); i++)
                    {
                        RoleAccessPermissionTbl roleAccessPermissionTbl = new RoleAccessPermissionTbl();
                        roleAccessPermissionTbl.RoleId       = role;
                        roleAccessPermissionTbl.AccessId     = result2.ToList()[i].AccessId;
                        roleAccessPermissionTbl.PermissionId = result2.ToList()[i].PermissionId;
                        db.RoleAccessPermissionTbls.Add(roleAccessPermissionTbl);
                    }
                    db.SaveChanges();
                }

                if (result2.Count() > 0)
                {
                    for (int i = 0; i < result2.Count(); i++)
                    {
                        AccessPermissionTbl accessPermissionTbl = new AccessPermissionTbl();
                        int access     = result2.ToList()[i].AccessId;
                        int permission = result2.ToList()[i].PermissionId;
                        int userid     = userId;
                        AccessPermissionTbl accessPermissionTbl1 = db.AccessPermissionTbls.Find(userid, access, permission);
                        db.AccessPermissionTbls.Remove(accessPermissionTbl1);
                    }
                    db.SaveChanges();
                }
                return(Ok(result10));
            }

            catch (Exception)
            {
                return(Content(HttpStatusCode.NotFound, "RoleId & UserId already exists"));
            }
        }
Exemplo n.º 2
0
        public IHttpActionResult AddRoleTblAndAccessPermissionTbl(UserDataModel userDataModel)
        {
            int latestRoleId = 0;

            try
            {
                string rolename = userDataModel.RoleName;
                var    result3  = (from a in db.RoleTbls
                                   where a.RoleName.Equals(userDataModel.RoleName)
                                   select new
                {
                    a.RoleName,
                    a.RoleId
                });
                if (result3.Count() > 0)
                {
                    for (int i = 0; i < result3.Count(); i++)
                    {
                        latestRoleId = result3.ToList()[i].RoleId;
                    }
                }

                else
                {
                    RoleTbl roleTbl = new RoleTbl();
                    roleTbl.RoleName = userDataModel.RoleName;

                    db.RoleTbls.Add(roleTbl);
                    db.SaveChanges();
                    latestRoleId = roleTbl.RoleId;
                }
                int userId = 0;

                var result4 = (from a in db.UserTbls
                               where a.UserId.Equals(userDataModel.UserId)
                               select new
                {
                    a.UserId,
                    a.DeletedDate
                });
                if (result4.Count() > 0)
                {
                    for (int i = 0; i < result4.Count(); i++)
                    {
                        if (result4.ToList()[i].DeletedDate == null && result4.ToList()[i].UserId == userDataModel.UserId)
                        {
                            userId = result4.ToList()[i].UserId;
                        }
                        else
                        {
                            return(Content(HttpStatusCode.NotFound, "This user account is not null else not there in UserTbl"));
                        }
                    }
                }
                else if (result4.Count() == 0)
                {
                    return(Content(HttpStatusCode.NotFound, "Userid is not exists"));
                }

                var result = (from a in db.AccessPermissionTbls
                              where a.UserId.Equals(userId)
                              select new
                {
                    a.AccessId,
                    a.PermissionId,
                    a.UserId
                }).ToList();

                var result1 = (from a in db.RoleAccessPermissionTbls
                               where a.RoleId.Equals(latestRoleId)
                               select new
                {
                    a.RoleId,
                    a.AccessId,
                    a.PermissionId
                }).ToList();

                if (result.Count() > 0)
                {
                    if (result1.Count() > 0)
                    {
                        for (int j = 0; j < result1.Count(); j++)
                        {
                            for (int i = 0; i < result.Count(); i++)
                            {
                                if (result.ToList()[i].AccessId == result1.ToList()[j].AccessId && result.ToList()[i].PermissionId == result1.ToList()[j].PermissionId)
                                {
                                    AccessPermissionTbl accessPermissiontbl = new AccessPermissionTbl();
                                    int userid1    = userId;
                                    int access     = result1.ToList()[j].AccessId;
                                    int permission = result1.ToList()[j].PermissionId;
                                    AccessPermissionTbl accessPermissionTbl1 = db.AccessPermissionTbls.Find(userid1, access, permission);
                                    db.AccessPermissionTbls.Remove(accessPermissionTbl1);
                                    db.SaveChanges();
                                    return(Ok("RoleId, AccessId and PermissionId these combination are already there " +
                                              "and click save again if you have given morethen one Accessname and Permission name..."));
                                }
                            }
                        }
                    }
                }

                if (result.Count() > 0)
                {
                    for (int i = 0; i < result.Count(); i++)
                    {
                        RoleAccessPermissionTbl roleAccessPermissionTbl = new RoleAccessPermissionTbl();

                        roleAccessPermissionTbl.AccessId     = result.ToList()[i].AccessId;
                        roleAccessPermissionTbl.PermissionId = result.ToList()[i].PermissionId;
                        roleAccessPermissionTbl.RoleId       = latestRoleId;
                        db.RoleAccessPermissionTbls.Add(roleAccessPermissionTbl);
                    }
                    db.SaveChanges();
                }

                if (result.Count() > 0)
                {
                    for (int j = 0; j < result.Count(); j++)
                    {
                        AccessPermissionTbl accessPermissiontbl = new AccessPermissionTbl();
                        int userid1    = result.ToList()[j].UserId;
                        int access     = result.ToList()[j].AccessId;
                        int permission = result.ToList()[j].PermissionId;
                        AccessPermissionTbl accessPermissionTbl1 = db.AccessPermissionTbls.Find(userid1, access, permission);
                        db.AccessPermissionTbls.Remove(accessPermissionTbl1);
                    }
                    db.SaveChanges();
                }
                else
                {
                    return(Content(HttpStatusCode.BadRequest, "insert record first in AccessPermissionTbl"));
                }
                //db.SaveChanges();
                return(Ok("Successfully Added"));
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.BadRequest, ex));
            }
        }