/// <summary> /// Create piranha User when user is login from external mean like Google /// </summary> /// <param name="user">Piranha.Entities.User</param> /// <returns></returns> private User CreateNewUser(Piranha.Entities.User user) { //get piranha Group Group group = null; using (var db = new Piranha.DataContext()) { string groupName = GetInitialGroupName(); group = db.Groups.Where(g => g.Name == groupName).FirstOrDefault(); } user.GroupId = group.Id; if (!String.IsNullOrEmpty(user.Password)) { user.Password = Piranha.Models.SysUserPassword.Encrypt(user.Password); } return(user); }
/// <summary> /// Login with Google account /// </summary> /// <param name="userModel"></param> /// <returns></returns> public ActionResult ExternalLogin(Piranha.Entities.User userModel) { if (ModelState.IsValid) { Piranha.Models.LoginModel loginModel = new LoginModel(); User user = GetUserByName(userModel.Login); loginModel.Login = userModel.Login; loginModel.Password = userModel.Password; if (user == null) { if (AllowGoogleLogin()) { user = CreateNewUser(userModel); using (var db = new Piranha.DataContext()) { // Login sysuser into the current context. db.LoginSys(); db.Users.Add(user); if (db.SaveChanges() > 0) { return(Login(loginModel)); } } } } else { //log he in using his gmail account -- user existed in our system return(Login(loginModel)); } } else { return(RedirectToAction("Login")); } return(Redirect("~/")); }