コード例 #1
0
        public bool DeleteRole(AppUser entity, int roleId, int updatedByUserId, ref string statusMessage)
        {
            bool isValid = true;

            if (entity == null || !IsValidGuid(entity.AspNetUserId))
            {
                statusMessage = "Error - please provide a value user identifier";
                return(false);
            }
            if (roleId < 1 || roleId > SiteReader)
            {
                statusMessage = "Error - please provide a value role identifier";
                return(false);
            }

            using (var context = new EntityContext())
            {
                try
                {
                    EM.AspNetUserRole efEntity = context.AspNetUserRoles
                                                 .SingleOrDefault(s => s.UserId == entity.AspNetUserId && s.RoleId == roleId.ToString());

                    if (efEntity != null && !string.IsNullOrWhiteSpace(efEntity.RoleId))
                    {
                        context.AspNetUserRoles.Remove(efEntity);
                        int count = context.SaveChanges();
                        if (count > 0)
                        {
                            isValid = true;
                            //TODO - add logging here or in the services
                        }
                    }
                    else
                    {
                        statusMessage = "Error - delete failed, as record was not found.";
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Account_DeleteRole(), Email: {0}", entity.Email));
                    statusMessage = ex.Message;
                    isValid       = false;
                }
            }

            return(isValid);
        }
コード例 #2
0
        public void UpdateRoles(string aspNetUserId, string[] roles)
        {
            using (var db = new EntityContext())
            {
                try
                {
                    var existRoles = db.AspNetUserRoles.Where(x => x.UserId == aspNetUserId.ToString());
                    var oldRoles   = existRoles.Select(x => x.RoleId).ToArray();

                    if (roles == null)
                    {
                        roles = new string[] { }
                    }
                    ;

                    //Add New Roles Selected
                    roles.Except(oldRoles).ToList().ForEach(x =>
                    {
                        var userRole = new EM.AspNetUserRole {
                            UserId = aspNetUserId, RoleId = x, Created = DateTime.Now
                        };
                        db.Entry(userRole).State = System.Data.Entity.EntityState.Added;
                    });

                    //Delete existing Roles unselected
                    existRoles.Where(x => !roles.Contains(x.RoleId)).ToList().ForEach(x =>
                    {
                        db.Entry(x).State = System.Data.Entity.EntityState.Deleted;
                    });

                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".UpdateRoles(), aspNetUserId: {0}", aspNetUserId));
                    //statusMessage = ex.Message;
                }
            }
        }
コード例 #3
0
        public bool AddRole(int userId, int roleId, int createdByUserId, ref string statusMessage)
        {
            bool   isValid      = true;
            string aspNetUserId = "";

            if (userId == 0)
            {
                statusMessage = "Error - please provide a valid user";
                return(false);
            }
            if (roleId < 1 || roleId > SiteReader)
            {
                statusMessage = "Error - please provide a valid role identifier";
                return(false);
            }

            AppUser user = AppUser_Get(userId);

            if (user != null && user.Id > 0)
            {
                aspNetUserId = user.AspNetUserId;
            }

            if (!IsValidGuid(aspNetUserId))
            {
                statusMessage = "Error - please provide a valid user identifier";
                return(false);
            }

            EM.AspNetUserRole efEntity = new EM.AspNetUserRole();
            using (var context = new EntityContext())
            {
                try
                {
                    efEntity.UserId  = aspNetUserId;
                    efEntity.RoleId  = roleId.ToString();
                    efEntity.Created = System.DateTime.Now;

                    context.AspNetUserRoles.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        statusMessage = "successful";
                        //other, maybe notification
                    }
                    else
                    {
                        //?no info on error
                        statusMessage = "Error - the Account_AddRole was not successful. ";
                        string message = string.Format("AccountManager. Account_AddRole Failed", "Attempted to add an Account_AddRole. The process appeared to not work, but was not an exception, so we have no message, or no clue. Email: {0}, roleId {1}, requestedBy: {2}", user.Email, roleId, createdByUserId);
                        EmailManager.NotifyAdmin("	Manager. Account_AddRole Failed", message);
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Account_AddRole(), Email: {0}", user.Email));
                    statusMessage = ex.Message;
                    isValid       = false;
                }
            }

            return(isValid);
        }