コード例 #1
0
        public void RoleSelect(UserRoleRequest roleRequest)
        {
            USER_ROLE userRole = new USER_ROLE()
            {
                USERID     = roleRequest.userId,
                ROLEID     = roleRequest.roleId,
                STATUS     = 1,
                INSERTDATE = DateTime.Now
            };

            _context.USER_ROLE.Add(userRole);
            _context.SaveChanges();
        }
コード例 #2
0
        public object AddRoleToUser(long UserId, int RoleId, string APP_ID)
        {
            if (UserId == 0 || RoleId == 0)
            {
                return(BadRequest("Values for UserId and RoleId are required.  Please verify your data."));
            }

            try
            {
                var executingUser = "";

                USER_ROLE urToAdd = new USER_ROLE()
                {
                    USER_ID = UserId, ROLE_ID = RoleId, IS_ACTIVE = "1", AUDIT_REC_CREATE_APPL_ID = APP_ID, AUDIT_REC_CREATE_DTS = DateTime.Now, AUDIT_REC_CREATE_APPL_USER_ID = "1", AUDIT_REC_CREATE_DB_USER_ID = "Not supposed to be done by API!"
                };
                USER_ROLE result = _service.AddRoleToUser(urToAdd);
                log.Info(string.Format("User {2} has added New RoleId: {0} association to UserId: {1}", RoleId, UserId, executingUser));
                return(Ok(result));
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    StringBuilder errmsg = new StringBuilder();

                    errmsg.Append(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                eve.Entry.Entity.GetType().Name, eve.Entry.State.ToString()));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        errmsg.Append(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                    ve.PropertyName, ve.ErrorMessage));
                    }
                    log.Error(e);
                }
                return(BadRequest("netApi.Controller.AddRoleToUser(int, int) caused DbEntityValidationException!"));
            }
            catch (Exception e)
            {
                var msg = string.Format("Error creating new User/Role association.\n\nUserId:\n{0}, RoleId: {1}.\n\n{2}", UserId, RoleId, e.Message);
                log.Error(e);

                return(BadRequest(msg));
            }
        }
コード例 #3
0
        public messageModel saveUserRole(RoleViewModel role)
        {
            messageModel result = new messageModel();

            try
            {
                if (String.IsNullOrEmpty(role.user_id))
                {
                    throw new Exception("Unauthorized Access");
                }
                var userId = JwtHelper.GetUserIdFromToken(role.user_id);
                if (String.IsNullOrEmpty(userId))
                {
                    throw new Exception("Unauthorized Access");
                }
                using (var context = new StandardCanEntities())
                {
                    var dt = DateTime.Now;
                    if (String.IsNullOrEmpty(role.usergroup_id))
                    {
                        var checkName = context.USER_GROUP.SingleOrDefault(a => a.Group_Name.ToUpper() == role.usergroup_Desc.ToUpper());
                        if (checkName != null)
                        {
                            throw new Exception("Data is duplicate");
                        }
                        USER_GROUP uSER_GROUP = new USER_GROUP();
                        uSER_GROUP.Group_Name = role.usergroup_Desc.ToUpper();
                        uSER_GROUP.Active     = role.active ?? false;
                        context.USER_GROUP.Add(uSER_GROUP);
                        context.SaveChanges();
                        if (role.program_list != null)
                        {
                            foreach (var item in role.program_list)
                            {
                                USER_ROLE data = new USER_ROLE();
                                data.Group_ID    = uSER_GROUP.Group_ID.ToString();
                                data.Program_ID  = item.program_id;
                                data.Active      = role.active ?? false;
                                data.Create_Date = dt;
                                data.Create_By   = Convert.ToInt32(userId);
                                data.Update_Date = dt;
                                data.Update_By   = Convert.ToInt32(userId);
                                context.USER_ROLE.Add(data);
                                context.SaveChanges();
                            }
                        }
                    }
                    else
                    {
                        var checkName = context.USER_GROUP.SingleOrDefault(a => a.Group_Name.ToUpper() == role.usergroup_Desc.ToUpper() && a.Group_ID.ToString() != role.usergroup_id);
                        if (checkName != null)
                        {
                            throw new Exception("Data is duplicate");
                        }
                        var userGroupDetail = context.USER_GROUP.SingleOrDefault(a => a.Group_ID.ToString() == role.usergroup_id);
                        if (userGroupDetail == null)
                        {
                            throw new Exception("Data is not found");
                        }
                        userGroupDetail.Group_Name = role.usergroup_Desc.ToUpper();
                        userGroupDetail.Active     = role.active ?? false;
                        context.SaveChanges();

                        var roleOldList = context.USER_ROLE.Where(a => a.Group_ID == role.usergroup_id).ToList();
                        context.USER_ROLE.RemoveRange(roleOldList);
                        context.SaveChanges();

                        if (role.program_list != null)
                        {
                            foreach (var item in role.program_list)
                            {
                                USER_ROLE data = new USER_ROLE();
                                data.Group_ID    = userGroupDetail.Group_ID.ToString();
                                data.Program_ID  = item.program_id;
                                data.Active      = role.active ?? false;
                                data.Create_Date = dt;
                                data.Create_By   = Convert.ToInt32(userId);
                                data.Update_Date = dt;
                                data.Update_By   = Convert.ToInt32(userId);
                                context.USER_ROLE.Add(data);
                                context.SaveChanges();
                            }
                        }
                    }
                }

                result.status  = "S";
                result.message = "";
            }
            catch (Exception ex)
            {
                result.status  = "E";
                result.message = ex.Message.ToString();
            }

            return(result);
        }
コード例 #4
0
 public USER_ROLE AddRoleToUser(USER_ROLE urToAdd)
 {
     return(_repository.AddRoleToUser(urToAdd));
 }