Ejemplo n.º 1
0
        public UserModel GetUserModel(string name, bool withMembers = false, string viewUser = null)
        {
            using (var ctx = new GitCandyContext())
            {
                var user = ctx.Users.FirstOrDefault(s => s.Name == name);

                if (user == null)
                    return null;

                var model = new UserModel
                {
                    Name = user.Name,
                    Nickname = user.Nickname,
                    Email = user.Email,
                    Description = user.Description,
                    IsSystemAdministrator = user.IsSystemAdministrator,
                };
                if (withMembers)
                {
                    model.Teams = ctx.UserTeamRoles
                        .Where(s => s.User.ID == user.ID)
                        .Select(s => s.Team.Name)
                        .AsEnumerable()
                        .OrderBy(s => s, new StringLogicalComparer())
                        .ToArray();

                    model.Respositories = ctx.UserRepositoryRoles
                        // belong user
                        .Where(s => s.User.ID == user.ID && s.IsOwner)
                        // can view for viewUser
                        .Where(s => !s.Repository.IsPrivate
                            || viewUser != null &&
                                (ctx.Users.Any(t => t.Name == viewUser && t.IsSystemAdministrator)
                                || ctx.UserRepositoryRoles.Any(t => t.RepositoryID == s.RepositoryID
                                    && t.User.Name == viewUser
                                    && t.AllowRead)
                                || ctx.TeamRepositoryRoles.Any(t => t.RepositoryID == s.RepositoryID
                                    && t.Team.UserTeamRoles.Any(r => r.User.Name == viewUser)
                                    && t.AllowRead)))
                        .Select(s => s.Repository.Name)
                        .AsEnumerable()
                        .OrderBy(s => s, new StringLogicalComparer())
                        .ToArray();
                }
                return model;
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(UserModel model)
        {
            if (ModelState.IsValid)
            {
                bool badName, badEmail;
                var user = MembershipService.CreateAccount(model.Name, model.Nickname, model.Password, model.Email, model.Description, out badName, out badEmail);
                if (user != null)
                {
                    if (Token != null)
                    {
                        return RedirectToAction("Detail", "Account", new { name = user.Name });
                    }
                    var auth = MembershipService.CreateAuthorization(user.ID, Token.AuthorizationExpires, Request.UserHostAddress);
                    Token = new Token(auth.AuthCode, user.ID, user.Name, user.Nickname, user.IsSystemAdministrator);
                    return RedirectToStartPage();
                }
                if (badName)
                    ModelState.AddModelError("Name", SR.Account_AccountAlreadyExists);
                if (badEmail)
                    ModelState.AddModelError("Email", SR.Account_EmailAlreadyExists);
            }

            return View(model);
        }
Ejemplo n.º 3
0
        public ActionResult Edit(string name, UserModel model)
        {
            if (string.IsNullOrEmpty(name))
                name = Token.Username;

            var isAdmin = Token.IsSystemAdministrator
                && !string.Equals(name, Token.Username, StringComparison.OrdinalIgnoreCase);

            ModelState.Remove("ConformPassword");
            if (ModelState.IsValid)
            {
                var user = MembershipService.Login(isAdmin ? Token.Username : name, model.Password);
                if (user != null)
                {
                    model.IsSystemAdministrator = Token.IsSystemAdministrator && model.IsSystemAdministrator;
                    if (!Token.IsSystemAdministrator || isAdmin || model.IsSystemAdministrator)
                    {
                        if (!MembershipService.UpdateUser(model))
                            throw new HttpException((int)HttpStatusCode.NotFound, string.Empty);
                        if (!isAdmin)
                        {
                            Token = MembershipService.GetToken(Token.AuthCode);
                        }

                        return RedirectToAction("Detail", "Account", new { name });
                    }
                    ModelState.AddModelError("IsSystemAdministrator", SR.Account_CantRemoveSelf);
                }
                else
                    ModelState.AddModelError("Password", SR.Account_PasswordError);
            }
            return View(model);
        }
Ejemplo n.º 4
0
        public bool UpdateUser(UserModel model)
        {
            using (var ctx = new GitCandyContext())
            {
                var user = ctx.Users.FirstOrDefault(s => s.Name == model.Name);
                if (user != null)
                {
                    user.Nickname = model.Nickname;
                    user.Email = model.Email;
                    user.Description = model.Description;
                    user.IsSystemAdministrator = model.IsSystemAdministrator;

                    ctx.SaveChanges();
                    return true;
                }
                return false;
            }
        }