how to edit aspnetuserroles using a different user table

1

I made a table of users, however I use helper to insert the same user in aspnetuser also, so I save a user I create a permission in both, how do I edit the permission of that?

 public async Task<ActionResult> Edit([Bind(Include = "UserId,UserName,FirstName,LastName,Analista,Parceiro,Vendedor")] User user, string rolename)
        {
            if (ModelState.IsValid)
            {
                var db2 = new MacPartnerContext();
                var currentUser = db2.Users.Find(user.UserId);
                if (currentUser.UserName != user.UserName)
                {   if (user.Analista)
                    {
                        UserHelper.UpRoleToUser(user.UserName, "Analista");
                    }

                    if (user.Parceiro)
                    {
                        UserHelper.UpRoleToUser(user.UserName, "Parceiro");
                    }

                    if (user.Vendedor)
                    {
                        UserHelper.UpRoleToUser(user.UserName, "Vendedor");
                    }
                UserHelper.UpdateUserName(currentUser.UserName,  user.UserName);
            }
                db2.Dispose();
                db.Entry(user).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(user);
        }
    
asked by anonymous 29.10.2018 / 14:04

2 answers

-1
public static bool UpdateUserName(string CorrentUserName,string _oldRoleName, string newRoleName, string newUserName)
        {
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
            var userAsp = userManager.FindByEmail(CorrentUserName);
            var oldRoleName = _oldRoleName;

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

            if (oldRoleName != newRoleName)
            {
                userManager.RemoveFromRole(userAsp.Id, oldRoleName);
                userManager.AddToRole(userAsp.Id, newRoleName);
            }

            userAsp.UserName = newUserName;
            userAsp.Email = newUserName;
            var response = userManager.Update(userAsp);
            return response.Succeeded;
        }

Controller:

// GET: Users/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = await db.Users.FindAsync(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        // POST: Users/Edit/5
        // Para se proteger de mais ataques, ative as propriedades específicas a que você quer se conectar. Para 
        // obter mais detalhes, consulte https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "UserId,UserName,FirstName,LastName,Analista,Parceiro,Vendedor,Financeiro, IsEn")] User user)
        {
            //Variável que guarda um objeto com o usuário antigo
            var db2 = new MacPartnerContext();
            var oldUser = db2.Users.Find(user.UserId);

            //variável que guarda o nome do Role antigo
            string oldRoleName = "";

            //Verificação do Role antigo
            if (oldUser.Analista)
            {
                oldRoleName = "Analista";
            }

            if (oldUser.Parceiro)
            {
                oldRoleName = "Parceiro";
            }

            if (oldUser.Vendedor)
            {
                oldRoleName = "Vendedor";
            }

            if (oldUser.Financeiro)
            {
                oldRoleName = "Financeiro";
            }

            //variável que guarda o nome do Role novo
            string newRole = "";

            //Verificação do Role novo
            if (user.Analista)
            {
                newRole = "Analista";
            }

            if (user.Parceiro)
            {
                newRole = "Parceiro";
            }

            if (user.Vendedor)
            {
                newRole = "Vendedor";
            }

            if (user.Financeiro)
            {
                newRole = "Financeiro";
            }

            if (ModelState.IsValid)
            {
                var currentUser = db2.Users.Find(user.UserId);

                UserHelper.UpdateUserName(currentUser.UserName, oldRoleName, newRole, user.UserName);

                db2.Dispose();

                db.Entry(user).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");

            }
            return View(user);
        }
    
04.12.2018 / 11:55
0

You need to customize Asp.net Identity by creating your own User and Permission entities and their respective repositories.

Do this by implementing the As.net Identity interfaces

for the Permission repository: IQueryableRoleStore

for the user repository: IUserRoleStore,  IUserPasswordStore,  IQueryableUserStore,  IUserStore,  IUserLockoutStore,  IUserTwoFactorStore

for the user entity:: IUser and for the permitting entity: IRole

The next step is to customize the authentication, user, and permission services inheriting the SignInManager, UserManager, and RoleManager classes respectively.

The TypeChave parameter is usually int, long or guid.

You can take a look at an example of a full authentication server using Asp.net + OWIN + OAuth + Asp.net Identity + Dapper in this my github repository

link

    
31.10.2018 / 15:17