コード例 #1
0
        public ActionResult AddMember(Guid id, AddMemberViewModel model)
        {
            if (ModelState.IsValid == false)
            {
                return View(model);
            }

            var db = this.DB();
            var account = db.Accounts.FirstOrDefault(_ => _.AccountName == model.Name && _.ProviderName == model.Provider);
            if (account == null)
            {
                account = new Account
                {
                    AccountName = model.Name,
                    ProviderName = model.Provider
                };
                db.Accounts.Add(account);
                db.SaveChanges();
            }

            var channel = this.DB().Channels.Find(id);
            if (channel.ChannelMembers.Any(_ => _.AccountId == account.Id))
            {
                ModelState.AddModelError("Name", "This account is already member of this channel.");
                return View(model);
            }

            channel.ChannelMembers.Add(new ChannelMember
            {
                AccountId = account.Id,
                IsOwner = model.IsOwner
            });
            db.SaveChanges();

            return RedirectToAction("Settings", new { id });
        }
コード例 #2
0
        internal ActionResult ExternalLoginCore(string provider, string providerUserId, string accountName)
        {
            var user = new Account
            {
                UniqueIdInProvider =
                    (providerUserId + "@" + provider)
                    .ToHashString<MD5>(AppSettings.Salt),
                ProviderName = provider,
                AccountName = accountName
            };

            var db = this.DB();
            var account = db.Accounts.FirstOrDefault(_ => _.ProviderName == user.ProviderName && _.AccountName == user.AccountName);
            if (account == null)
            {
                db.Accounts.Add(user);
            }
            else if (account.UniqueIdInProvider == "")
            {
                account.UniqueIdInProvider = user.UniqueIdInProvider;
            }
            db.SaveChanges();

            this.HttpContext.SetAuthCookie(user.UniqueIdInProvider, user.AccountName);

            return RedirectToAction("List", "Channel");
        }