/// <summary> /// This static constructor is called only one time, when the application is started. /// It synchronizes the features available for each service with the features available in the database. /// </summary> static SocialTFSProxy() { ConnectorDataContext db = new ConnectorDataContext(); XmlConfigurator.Configure(new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~/log4net.config"))); //add the completely new features IEnumerable<FeaturesType> features = FeaturesManager.GetFeatures(); foreach (FeaturesType featureType in features) { Stopwatch w = Stopwatch.StartNew(); bool feat = db.Features.Contains(new Feature() { name = featureType.ToString() }); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", feature's name: " + featureType.ToString() + ", check if the feature is available when the application is started"); if (!feat) { Stopwatch w1 = Stopwatch.StartNew(); db.Features.InsertOnSubmit(new Feature() { name = featureType.ToString(), description = FeaturesManager.GetFeatureDescription(featureType), @public = FeaturesManager.IsPublicFeature(featureType) }); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", feature's name: " + featureType.ToString() + ", description: " + FeaturesManager.GetFeatureDescription(featureType) + ", public: " + FeaturesManager.IsPublicFeature(featureType) + ", insert a feature in a pending state"); } } Stopwatch w2 = Stopwatch.StartNew(); db.SubmitChanges(); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", insert the feature"); }
private void DeleteService(int id) { ConnectorDataContext db = new ConnectorDataContext(); bool isDeleted; try { Stopwatch w1 = Stopwatch.StartNew(); db.ServiceInstances.DeleteAllOnSubmit(db.ServiceInstances.Where(si => si.id == id)); db.SubmitChanges(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", remove all service instances"); isDeleted = true; } catch (Exception) { isDeleted = false; } XDocument xml = new XDocument( new XElement("Root", new XElement("Deleted", isDeleted))); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xml); Response.End(); }
protected void Page_Load(object sender, EventArgs e) { ConnectorDataContext db = new ConnectorDataContext(); Series userSeries = RegisteredUser.Series[0]; userSeries["PieLabelStyle"] = "Outside"; userSeries.Points.Clear(); Stopwatch w1 = Stopwatch.StartNew(); int countActiveUsers = db.Users.Where(u => u.active && !u.isAdmin).Count(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", count the number of registered users"); userSeries.Points.AddXY("Registered", countActiveUsers); Stopwatch w2 = Stopwatch.StartNew(); int countNotActiveUsers = db.Users.Where(u => !u.active && !u.isAdmin).Count(); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", count the number of unregistered users"); userSeries.Points.AddXY("Unregistered", countNotActiveUsers); Series serviceSeries = RegisteredService.Series[0]; serviceSeries["PieLabelStyle"] = "Outside"; serviceSeries.Points.Clear(); Stopwatch w = Stopwatch.StartNew(); List<ServiceInstance> sInstances = db.ServiceInstances.Where(si => si.name != "SocialTFS").ToList(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select all service instances different from 'SocialTFS'"); foreach (ServiceInstance item in sInstances) serviceSeries.Points.AddXY(item.name, item.Registrations.Count); }
private void LoadWeights() { ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); List<FeatureScore> fScores = db.FeatureScores.ToList(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select all feature scores"); foreach (var item in fScores) { HtmlTableCell service = new HtmlTableCell(); HtmlTableCell feature = new HtmlTableCell(); HtmlTableCell weight = new HtmlTableCell(); service.InnerText = item.ServiceInstance.name; feature.InnerText = item.feature; weight.InnerText = item.score.ToString(); weight.Attributes.Add("class", "center"); weight.Attributes.Add("contenteditable", "true"); HtmlTableRow tr = new HtmlTableRow(); tr.Cells.Add(service); tr.Cells.Add(feature); tr.Cells.Add(weight); WeightTable.Rows.Add(tr); } }
/// <summary> /// Send an email. /// </summary> /// <param name="to">Addressee.</param> /// <param name="subject">Sunject.</param> /// <param name="body">Message.</param> /// <param name="isBodyHtml">True if the message is wrote in HTML.</param> /// <returns>True if the email is correctly sended, false otherwise.</returns> public static bool SendEmail(String to, String subject, String body, bool isBodyHtml) { try { ConnectorDataContext db = new ConnectorDataContext(); MailMessage message = new MailMessage(); message.To.Add(new MailAddress(to)); Stopwatch w = Stopwatch.StartNew(); message.From = new MailAddress(db.Settings.Where(s => s.key == "MailAddress").Single().value, "SocialTFS"); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select the sending mail address"); message.Subject = subject; message.IsBodyHtml = isBodyHtml; message.Body = body; Stopwatch w1 = Stopwatch.StartNew(); SmtpClient smtp = new SmtpClient(db.Settings.Where(s => s.key == "SmtpServer").Single().value, Int32.Parse(db.Settings.Where(s => s.key == "SmtpPort").Single().value)); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", select the value of 'SmtpServer' key"); Stopwatch w2 = Stopwatch.StartNew(); String smtpSec = db.Settings.Where(s => s.key == "SmtpSecurity").Single().value; w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", select the value of 'SmtpSecurity' key"); switch (smtpSec) { case "None": break; case "SSL/TLS": smtp.UseDefaultCredentials = false; smtp.EnableSsl = true; Stopwatch w3 = Stopwatch.StartNew(); smtp.Credentials = new NetworkCredential(db.Settings.Where(s => s.key == "MailAddress").Single().value, db.EncDecRc4("key", db.Settings.Where(s => s.key == "MailPassword").Single().value)); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", select smtp credentials(SSL/TLS)"); break; case "STARTTLS": smtp.UseDefaultCredentials = false; smtp.EnableSsl = true; Stopwatch w4 = Stopwatch.StartNew(); smtp.Credentials = new NetworkCredential(db.Settings.Where(s => s.key == "MailAddress").Single().value, db.EncDecRc4("key", db.Settings.Where(s => s.key == "MailPassword").Single().value), ""); w4.Stop(); ILog log4 = LogManager.GetLogger("QueryLogger"); log4.Info(" Elapsed time: " + w4.Elapsed + ", select smtp credentials(STARTTLS)"); break; } smtp.Send(message); return true; } catch { return false; } }
private void DeleteUser(int id) { ConnectorDataContext db = new ConnectorDataContext(); bool isDeleted; string errorMessage = String.Empty; try { Stopwatch w = Stopwatch.StartNew(); db.InteractiveFriends.DeleteAllOnSubmit(db.InteractiveFriends.Where(q => q.user == id)); db.SubmitChanges(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", remove all interactive friends of an user"); Stopwatch w2 = Stopwatch.StartNew(); db.DynamicFriends.DeleteAllOnSubmit(db.DynamicFriends.Where(q => q.user == id)); db.SubmitChanges(); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", remove all dynamic friends of an user"); Stopwatch w3 = Stopwatch.StartNew(); db.StaticFriends.DeleteAllOnSubmit(db.StaticFriends.Where(q => q.user == id)); db.SubmitChanges(); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", remove all static friends of an user"); Stopwatch w4 = Stopwatch.StartNew(); db.Suggestions.DeleteAllOnSubmit(db.Suggestions.Where(q => q.user == id)); db.SubmitChanges(); w4.Stop(); ILog log4 = LogManager.GetLogger("QueryLogger"); log4.Info(" Elapsed time: " + w4.Elapsed + ", remove all suggestions of an user"); Stopwatch w1 = Stopwatch.StartNew(); db.Users.DeleteAllOnSubmit(db.Users.Where(u => u.id == id)); db.SubmitChanges(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", remove the user"); isDeleted = true; } catch (Exception e) { errorMessage = e.Message; // errorMessage = e.StackTrace; isDeleted = false; } XDocument xml = new XDocument( new XElement("Root", new XElement("Deleted", isDeleted), new XElement("Errors", errorMessage))); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xml); Response.End(); }
private void ChangeAdminSettings() { string username = Request.Params["ctl00$MainContent$AdminUsernameTB"]; string email = Request.Params["ctl00$MainContent$AdminEmailTB"]; string password = Request.Params["ctl00$MainContent$PasswordTB"]; string confirm = Request.Params["ctl00$MainContent$ConfirmTB"]; ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); User admin = db.Users.Where(u => u.isAdmin).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select the admin to change his settings"); bool changePassword = true; if (ChangePasswordCB.Checked) if (password.Equals(confirm)) admin.password = db.Encrypt(password); else { ErrorPA.Attributes.Add("class", "error"); ErrorPA.InnerText = "Passwords do not match."; changePassword = false; } if (changePassword) { Stopwatch w2 = Stopwatch.StartNew(); bool usr = db.Users.Any(u => (u.username == username || u.email == email) && !u.isAdmin); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", check if there is an user with admin's username or email"); if (!usr) { admin.username = username; admin.email = email; Stopwatch w3 = Stopwatch.StartNew(); db.SubmitChanges(); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", change admin settings"); ErrorPA.Attributes.Add("class", "confirm"); ErrorPA.InnerText = "Data stored"; } else { ErrorPA.Attributes.Add("class", "error"); ErrorPA.InnerText = "Username or email already exist."; } } }
private void SaveUsers() { ConnectorDataContext db = new ConnectorDataContext(); XmlDocument requestXml = new XmlDocument(); requestXml.Load(new XmlTextReader(new StreamReader(Request.InputStream))); List<string> mailError = new List<string>(); foreach (XmlNode item in requestXml.SelectNodes("//users/user")) { try { String passwd = Membership.GeneratePassword(10, 2); User user = new User() { username = item.InnerText, email = item.InnerText, password = db.Encrypt(passwd) }; Stopwatch w = Stopwatch.StartNew(); db.Users.InsertOnSubmit(user); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", insert the user in a pending state"); if (WebUtility.SendEmail(item.InnerText, "SocialCDE invitation", GetBody(item.InnerText, passwd), true)) { Stopwatch w1 = Stopwatch.StartNew(); db.SubmitChanges(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", send mail for registration"); } else mailError.Add(item.InnerText); } catch { mailError.Add(item.InnerText); } } XElement root = new XElement("Root"); foreach (string item in mailError) root.Add(new XElement("NotSent", item)); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(new XDocument(root)); Response.End(); }
private void LoadServices() { ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); List<ServiceInstance> sInstance = db.ServiceInstances.Where(s => s.Service.name != "SocialTFS").ToList(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select all service instances different from 'SocialTFS' to load them"); foreach (var item in sInstance) { HtmlTableCell name = new HtmlTableCell(); HtmlTableCell service = new HtmlTableCell(); HtmlTableCell host = new HtmlTableCell(); HtmlTableCell edit = new HtmlTableCell(); HtmlTableCell delete = new HtmlTableCell(); name.InnerText = item.name; service.InnerText = item.Service.name; host.InnerText = item.host; IService iService = ServiceFactory.getService(item.Service.name); if (iService.GetPrivateFeatures().Contains(FeaturesType.MoreInstance) || iService.GetPrivateFeatures().Contains(FeaturesType.Labels)) { HtmlInputButton editBT = new HtmlInputButton(); editBT.Attributes.Add("title", "Edit " + item.name); editBT.Attributes.Add("class", "edit"); editBT.Value = item.id.ToString(); edit.Attributes.Add("class", "center"); edit.Controls.Add(editBT); } HtmlInputButton deleteBT = new HtmlInputButton(); deleteBT.Attributes.Add("title", "Delete " + item.name); deleteBT.Attributes.Add("class", "delete"); deleteBT.Value = item.id.ToString(); delete.Attributes.Add("class", "center"); delete.Controls.Add(deleteBT); HtmlTableRow tr = new HtmlTableRow(); tr.ID = "Row" + item.id; tr.Cells.Add(name); tr.Cells.Add(service); tr.Cells.Add(host); tr.Cells.Add(edit); tr.Cells.Add(delete); ServiceTable.Rows.Add(tr); } }
private void PopulateService() { ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); ServiceInstance service = (from serin in db.ServiceInstances where serin.id == Int32.Parse(Request.QueryString["id"]) select serin).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select the service instances to edit"); IService iService = ServiceFactory.getService(service.Service.name); if (!iService.GetPrivateFeatures().Contains(FeaturesType.MoreInstance) && !iService.GetPrivateFeatures().Contains(FeaturesType.Labels)) Response.Redirect("Services.aspx"); Id.Value = service.id.ToString(); ServiceTB.Value = service.Service.name; NameTB.Value = service.name; HostTB.Value = service.host; if (iService.GetPrivateFeatures().Contains(FeaturesType.OAuth1)) { ConsumerKeyTB.Value = service.consumerKey; ConsumerSecretTB.Value = service.consumerSecret; } else { ConsumerKeyTB.Attributes["required"] = String.Empty; ConsumerSecretTB.Attributes["required"] = String.Empty; ConsumerKeyRW.Visible = false; ConsumerSecretRW.Visible = false; } if (!iService.GetPrivateFeatures().Contains(FeaturesType.Labels)) { GitHubLabelRW.Visible = false; ErrGitHubLabelRW.Visible = false; } else { ServiceTB.Disabled = true; NameTB.Disabled = true; HostTB.Disabled = true; GitHubLabelTB.Value = ServiceFactory.GitHubLabels; ErrGitHubLabelRW.Visible = true; } }
/// <summary> /// Convert a Post (used for the database) in a WPost (used for the web). /// </summary> /// <param name="db">Database connector data context.</param> /// <param name="user">User that requires the conversion.</param> /// <param name="post">The Post to convert.</param> /// <returns>A WPost.</returns> public static WPost PostToWPost(ConnectorDataContext db, User user, Post post) { WUser author = Converter.UserToWUser(db, user, post.ChosenFeature.Registration.User, false); WService service = Converter.ServiceInstanceToWService(db, user, post.ChosenFeature.Registration.ServiceInstance, false); WPost result = new WPost() { Id = post.id, User = author, Service = service, Message = post.message, CreateAt = post.createAt }; return result; }
private void SaveWeights() { ConnectorDataContext db = new ConnectorDataContext(); bool isSaved; XmlDocument requestXml = new XmlDocument(); requestXml.Load(new XmlTextReader(new StreamReader(Request.InputStream))); try { foreach (XmlNode item in requestXml.SelectNodes("//weights/item")) { Stopwatch w = Stopwatch.StartNew(); FeatureScore featureScore = db.FeatureScores.Where(fs => fs.ServiceInstance.name == item.SelectSingleNode("service").InnerText && fs.feature == item.SelectSingleNode("feature").InnerText).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select feature scores"); featureScore.score = Int32.Parse(item.SelectSingleNode("weight").InnerText); } Stopwatch w1 = Stopwatch.StartNew(); db.SubmitChanges(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", save weights"); isSaved = true; } catch (Exception) { isSaved = false; } XDocument xml = new XDocument( new XElement("Root", new XElement("Saved", isSaved))); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xml); Response.End(); }
private void PopulateServices() { ServiceSE.Items.Add(new ListItem()); ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); List<Service> serv = db.Services.Where(s => s.name != "SocialTFS").ToList(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select all services different from 'SocialTFS'"); foreach (Service item in serv) { IService iService = ServiceFactory.getService(item.name); Stopwatch w1 = Stopwatch.StartNew(); bool servInstance = db.ServiceInstances.Select(si => si.service).Contains(item.id); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", check if the service instance contains the service"); if (iService.GetPrivateFeatures().Contains(FeaturesType.MoreInstance) || !servInstance) ServiceSE.Items.Add(new ListItem(item.name, item.id.ToString())); } }
private void ChangeSmtpSettings() { ConnectorDataContext db = new ConnectorDataContext(); try { bool changePassword = true; if (ChangeMailPasswordCB.Checked) { if (Request.Params["ctl00$MainContent$MailPasswordTB"].Equals(Request.Params["ctl00$MainContent$MailConfirmTB"])) { Stopwatch w2 = Stopwatch.StartNew(); db.Settings.Where(s => s.key == "MailPassword").Single().value = db.EncDecRc4("key", Request.Params["ctl00$MainContent$MailPasswordTB"]); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", select the value of 'MailPassword' key from settings"); } else { ErrorPA.Attributes.Add("class", "error"); ErrorPA.InnerText = "Passwords do not match."; changePassword = false; } } if (changePassword) { Stopwatch w3 = Stopwatch.StartNew(); db.Settings.Where(s => s.key == "SmtpServer").Single().value = Request.Params["ctl00$MainContent$SmtpServerTB"]; w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", set the value of 'Smtp Server' key"); Stopwatch w4 = Stopwatch.StartNew(); db.Settings.Where(s => s.key == "SmtpPort").Single().value = Request.Params["ctl00$MainContent$SmtpPortTB"]; w4.Stop(); ILog log4 = LogManager.GetLogger("QueryLogger"); log4.Info(" Elapsed time: " + w4.Elapsed + ", set the value of 'Smtp Port' key"); Stopwatch w5 = Stopwatch.StartNew(); db.Settings.Where(s => s.key == "SmtpSecurity").Single().value = Request.Params["ctl00$MainContent$SmtpSecuritySE"]; w5.Stop(); ILog log5 = LogManager.GetLogger("QueryLogger"); log5.Info(" Elapsed time: " + w5.Elapsed + ", set the value of 'Smtp Security' key"); Stopwatch w6 = Stopwatch.StartNew(); db.Settings.Where(s => s.key == "MailAddress").Single().value = Request.Params["ctl00$MainContent$MailAddressTB"]; w6.Stop(); ILog log6 = LogManager.GetLogger("QueryLogger"); log6.Info(" Elapsed time: " + w6.Elapsed + ", set the value of 'MailAddress' key"); Stopwatch w7 = Stopwatch.StartNew(); db.SubmitChanges(); w7.Stop(); ILog log7 = LogManager.GetLogger("QueryLogger"); log7.Info(" Elapsed time: " + w7.Elapsed + ", change smtp settings"); ErrorPA.Attributes.Add("class", "confirm"); ErrorPA.InnerText = "Data stored."; } } catch { try { Stopwatch w8 = Stopwatch.StartNew(); db.Settings.InsertAllOnSubmit(new List<Setting>(){ new Setting () { key = "SmtpServer", value = Request.Params["ctl00$MainContent$SmtpServerTB"] }, new Setting () { key = "SmtpPort", value = Request.Params["ctl00$MainContent$SmtpPortTB"] }, new Setting () { key = "SmtpSecurity", value = Request.Params["ctl00$MainContent$SmtpSecuritySE"] }, new Setting () { key = "MailAddress", value = Request.Params["ctl00$MainContent$MailAddressTB"] }, new Setting () { key = "MailPassword", value = db.EncDecRc4("key",Request.Params["ctl00$MainContent$MailPasswordTB"]) } }); db.SubmitChanges(); w8.Stop(); ILog log8 = LogManager.GetLogger("QueryLogger"); log8.Info(" Elapsed time: " + w8.Elapsed + ", insert new settings"); ErrorPA.Attributes.Add("class", "confirm"); ErrorPA.InnerText = "Data stored."; } catch { ErrorPA.Attributes.Add("class", "error"); ErrorPA.InnerText = "Something was wrong. Please try again later."; } } }
public bool IsAvailable(String username) { Contract.Requires(!String.IsNullOrEmpty(username)); ConnectorDataContext db = new ConnectorDataContext(); try { Stopwatch w = Stopwatch.StartNew(); User user = db.Users.Where(u => u.username == username && u.active).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", username: "******", select an username to check if it is already used"); return false; } catch (InvalidOperationException) { return true; } }
private void FillSmtpSettings() { ConnectorDataContext db = new ConnectorDataContext(); try { Stopwatch w9 = Stopwatch.StartNew(); SmtpServerTB.Value = db.Settings.Where(s => s.key == "SmtpServer").Single().value; w9.Stop(); ILog log9 = LogManager.GetLogger("QueryLogger"); log9.Info(" Elapsed time: " + w9.Elapsed + ", select the value of 'SmtpServer' key from settings"); Stopwatch w10 = Stopwatch.StartNew(); SmtpPortTB.Value = db.Settings.Where(s => s.key == "SmtpPort").Single().value; w10.Stop(); ILog log10 = LogManager.GetLogger("QueryLogger"); log10.Info(" Elapsed time: " + w10.Elapsed + ", select the value of 'SmtpPort' key from settings"); Stopwatch w11 = Stopwatch.StartNew(); SmtpSecuritySE.Value = db.Settings.Where(s => s.key == "SmtpSecurity").Single().value; w11.Stop(); ILog log11 = LogManager.GetLogger("QueryLogger"); log11.Info(" Elapsed time: " + w11.Elapsed + ", select the value of 'SmtpSecurity' key from settings"); Stopwatch w12 = Stopwatch.StartNew(); MailAddressTB.Value = db.Settings.Where(s => s.key == "MailAddress").Single().value; w12.Stop(); ILog log12 = LogManager.GetLogger("QueryLogger"); log12.Info(" Elapsed time: " + w12.Elapsed + ", select the value of 'MailAddress' key from settings"); } catch { } }
private void FillAdminSettings() { ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); User admin = db.Users.Where(u => u.isAdmin).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select the admin to fill his settings"); AdminUsernameTB.Value = admin.username; AdminEmailTB.Value = admin.email; }
private void CheckUsername(string username) { ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); bool usr = db.Users.Any(u => u.username == username && !u.isAdmin); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", check if the username is already used"); XDocument xml = new XDocument( new XElement("Root", new XElement("IsAviable", !usr))); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xml); Response.End(); }
public int SubscribeUser(String email, String password, String username) { Contract.Requires(!String.IsNullOrEmpty(email)); Contract.Requires(!String.IsNullOrEmpty(password)); Contract.Requires(!String.IsNullOrEmpty(username)); ConnectorDataContext db = new ConnectorDataContext(); User user; try { Stopwatch w = Stopwatch.StartNew(); user = db.Users.Where(u => u.email == email).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", user email: " + email + ", select the user to subscribe him"); } catch (InvalidOperationException) { return 1; } if (user.password != db.Encrypt(password)) return 2; if (!IsAvailable(username)) return 3; user.username = username; user.active = true; Stopwatch w1 = Stopwatch.StartNew(); int sInstance = db.ServiceInstances.Where(si => si.Service.name == "SocialTFS").Single().id; w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", select the service instance with name 'SocialTFS'"); Registration registration = new Registration() { User = user, serviceInstance = sInstance, nameOnService = username, idOnService = username }; Stopwatch w2 = Stopwatch.StartNew(); db.Registrations.InsertOnSubmit(registration); db.SubmitChanges(); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", service instance's id: " + sInstance + ", name and id on service: " + username + ", insert a new registration"); Stopwatch w3 = Stopwatch.StartNew(); db.ChosenFeatures.InsertOnSubmit(new ChosenFeature() { Registration = registration, feature = FeaturesType.Post.ToString(), lastDownload = new DateTime(1900, 1, 1) }); db.SubmitChanges(); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", feature: " + FeaturesType.Post.ToString() + ", last download: " + new DateTime(1900, 1, 1) + ", insert a new Chosen feature"); return 0; }
private void SaveService() { ConnectorDataContext db = new ConnectorDataContext(); Service service = new Service(); try { Stopwatch w3 = Stopwatch.StartNew(); service = db.Services.Where(s => s.id == Int32.Parse(Request.Params["ctl00$MainContent$ServiceSE"])).Single(); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", select the service to save it"); } catch { ErrorPA.Style.Remove("display"); } IService iService = ServiceFactory.getService(service.name); ServiceInstance serviceInstance = new ServiceInstance(); if (!iService.GetPrivateFeatures().Contains(FeaturesType.MoreInstance)) { Stopwatch w4 = Stopwatch.StartNew(); PreregisteredService preser = db.PreregisteredServices.Where(ps => ps.service == service.id).Single(); w4.Stop(); ILog log4 = LogManager.GetLogger("QueryLogger"); log4.Info(" Elapsed time: " + w4.Elapsed + ", select the preregistered service to save the service"); serviceInstance.name = preser.name; serviceInstance.host = preser.host; serviceInstance.service = preser.service; serviceInstance.consumerKey = preser.consumerKey; serviceInstance.consumerSecret = preser.consumerSecret; Stopwatch w5 = Stopwatch.StartNew(); db.ServiceInstances.InsertOnSubmit(serviceInstance); w5.Stop(); ILog log5 = LogManager.GetLogger("QueryLogger"); log5.Info(" Elapsed time: " + w5.Elapsed + ", insert the service instance in a pending state"); } else { string consumerKey = null, consumerSecret = null; string host = Request.Params["ctl00$MainContent$HostTB"]; if (host.EndsWith(@"/")) host = host.Remove(host.Length - 1); if (iService.GetPrivateFeatures().Contains(FeaturesType.OAuth1)) { consumerKey = Request.Params["ctl00$MainContent$ConsumerKeyTB"]; consumerSecret = Request.Params["ctl00$MainContent$ConsumerSecretTB"]; } serviceInstance.name = Request.Params["ctl00$MainContent$NameTB"]; serviceInstance.host = host; serviceInstance.service = service.id; serviceInstance.consumerKey = consumerKey; serviceInstance.consumerSecret = consumerSecret; Stopwatch w6 = Stopwatch.StartNew(); db.ServiceInstances.InsertOnSubmit(serviceInstance); w6.Stop(); ILog log6 = LogManager.GetLogger("QueryLogger"); log6.Info(" Elapsed time: " + w6.Elapsed + ", insert the service instance in a pending state"); } Stopwatch w7 = Stopwatch.StartNew(); db.SubmitChanges(); w7.Stop(); ILog log7 = LogManager.GetLogger("QueryLogger"); log7.Info(" Elapsed time: " + w7.Elapsed + ", insert the service "); if (iService.GetPrivateFeatures().Contains(FeaturesType.Labels)) { iService.Get(FeaturesType.Labels, Request.Params["ctl00$MainContent$GitHubLabelTB"]); } foreach (FeaturesType featureType in iService.GetScoredFeatures()) { Stopwatch w8 = Stopwatch.StartNew(); db.FeatureScores.InsertOnSubmit(new FeatureScore() { serviceInstance = serviceInstance.id, feature = featureType.ToString(), score = 1 }); w8.Stop(); ILog log8 = LogManager.GetLogger("QueryLogger"); log8.Info(" Elapsed time: " + w8.Elapsed + ", insert the relative feature score in a pending state"); } //TODO update the new version (leave comment from the next line) //dbService.version = newServiceVersion; Stopwatch w9 = Stopwatch.StartNew(); db.SubmitChanges(); w9.Stop(); ILog log9 = LogManager.GetLogger("QueryLogger"); log9.Info(" Elapsed time: " + w9.Elapsed + ", insert the feature score"); Response.Redirect("Services.aspx"); }
private void ServiceFields() { ConnectorDataContext db = new ConnectorDataContext(); try { Stopwatch w2 = Stopwatch.StartNew(); String serv = db.Services.Where(s => s.id == Int32.Parse(Request.QueryString["id"])).Single().name; w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", select the name of the service"); IService iService = ServiceFactory.getService(serv); XDocument xml = new XDocument( new XElement("Root", new XElement("CanHaveMoreInstance", iService.GetPrivateFeatures().Contains(FeaturesType.MoreInstance)), new XElement("NeedOAuth", iService.GetPrivateFeatures().Contains(FeaturesType.OAuth1)), new XElement("NeedGitHubLabel", iService.Name.Equals("GitHub")))); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xml); Response.End(); } catch (TargetInvocationException) { XDocument xml = new XDocument( new XElement("Root", new XElement("CanHaveMoreInstance", false), new XElement("NeedOAuth", false), new XElement("NeedGitHubLabel", false))); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xml); Response.End(); } catch (InvalidOperationException) { Response.Redirect("Services.aspx"); } }
public bool RecordService(string username, string password, int service, string usernameOnService, string passwordOnService, string domain) { Contract.Requires(!String.IsNullOrEmpty(username)); Contract.Requires(!String.IsNullOrEmpty(password)); Contract.Requires(!String.IsNullOrEmpty(usernameOnService)); ConnectorDataContext db = new ConnectorDataContext(); User user = CheckCredentials(db, username, password); if (user == null) return false; Stopwatch w = Stopwatch.StartNew(); ServiceInstance serviceInstance = db.ServiceInstances.Where(s => s.id == service).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", service id: " + service + ", record a service without OAuth authentication procedure"); IService iService = ServiceFactory.getService( serviceInstance.Service.name, usernameOnService, passwordOnService, domain, serviceInstance.host); IUser iUser = iService.VerifyCredential(); return RegisterUserOnAService(db, user, serviceInstance, iUser, db.EncDecRc4("key", passwordOnService), (String)iUser.Get(UserFeaturesType.Domain)); }
public bool SaveAvatar(string username, string password, Uri avatar) { Contract.Requires(!String.IsNullOrEmpty(username)); Contract.Requires(!String.IsNullOrEmpty(password)); ConnectorDataContext db = new ConnectorDataContext(); User user = CheckCredentials(db, username, password); if (user == null) return false; Stopwatch w = Stopwatch.StartNew(); user.avatar = avatar.AbsoluteUri; db.SubmitChanges(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", uri: " + avatar.AbsoluteUri + ", save avatar"); return true; }
protected void Page_Load(object sender, EventArgs e) { ConnectorDataContext db = new ConnectorDataContext(); String token = Request.QueryString["token"]; Setting recoveringToken = null; Setting recoveringTime = null; try { Stopwatch w = Stopwatch.StartNew(); recoveringTime = db.Settings.Where(s => s.key == "RecoveringTime").Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select the 'recovering time' key from settings"); Stopwatch w1 = Stopwatch.StartNew(); recoveringToken = db.Settings.Where(s => s.key == "RecoveringToken").Single(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", select the 'recovering token' key from settings"); } catch { } if (Request.RequestType == "GET") { if (String.IsNullOrEmpty(token)) { if (recoveringTime == null || DateTime.Parse(recoveringTime.value) < DateTime.UtcNow - new TimeSpan(0, 5, 0)) { String newToken = GenerateToken(); Stopwatch w2 = Stopwatch.StartNew(); String to = db.Users.Where(u => u.isAdmin).Single().email; w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", select the admin's email"); if (WebUtility.SendEmail(to, "Password recovering", GetBody(newToken), true)) { if (recoveringToken != null) { recoveringToken.value = newToken; recoveringTime.value = DateTime.UtcNow.ToString(); } else { Stopwatch w3 = Stopwatch.StartNew(); db.Settings.InsertAllOnSubmit( new List<Setting>(){ new Setting () { key = "RecoveringToken", value = newToken }, new Setting () { key = "RecoveringTime", value = DateTime.UtcNow.ToString() }}); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", insert new setting in a pending state(password recovering)"); } Stopwatch w4 = Stopwatch.StartNew(); db.SubmitChanges(); w4.Stop(); ILog log4 = LogManager.GetLogger("QueryLogger"); log4.Info(" Elapsed time: " + w4.Elapsed + ", insert new settings"); Response.Redirect("Login.aspx?type=confirm&message=Email sent, check your email inbox."); } else Response.Redirect("Login.aspx?type=error&message=Is not possible recover the password, the smtp server is not set."); } else Response.Redirect("Login.aspx?type=error&message=You have sent a request less than 5 minutes ago. Please, try again later."); } else { if (recoveringToken == null || recoveringToken.value != token) Response.Redirect("Login.aspx?type=error&message=Wrong token."); } } else if (Request.RequestType == "POST") { Stopwatch w5 = Stopwatch.StartNew(); db.Users.Where(u => u.isAdmin).Single().password = db.Encrypt(Request.Params["ctl00$MainContent$PasswordTB"]); w5.Stop(); ILog log5 = LogManager.GetLogger("QueryLogger"); log5.Info(" Elapsed time: " + w5.Elapsed + ", select admin's password"); Stopwatch w6 = Stopwatch.StartNew(); db.Settings.DeleteAllOnSubmit(db.Settings.Where(s => s.key == "RecoveringToken" || s.key == "RecoveringTime")); db.SubmitChanges(); w6.Stop(); ILog log6 = LogManager.GetLogger("QueryLogger"); log6.Info(" Elapsed time: " + w6.Elapsed + ", password changed"); Response.Redirect("Login.aspx?type=confirm&message=Password changed successfully."); } }
private void LoadPage(int page) { ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w = Stopwatch.StartNew(); var users = (from usr in db.Users where !usr.isAdmin orderby usr.username select usr).Skip((page - 1) * userPerPage).Take(userPerPage); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", select all users"); Stopwatch w11 = Stopwatch.StartNew(); bool utente=users.Any(); w11.Stop(); ILog log11 = LogManager.GetLogger("QueryLogger"); log11.Info(" Elapsed time: " + w11.Elapsed + ", check if the select user query returns at least one user"); if (!utente && page > 1) Response.Redirect("Users.aspx?page=" + (page - 1).ToString()); foreach (var item in users) { HtmlTableCell username = new HtmlTableCell(); HtmlTableCell email = new HtmlTableCell(); HtmlTableCell active = new HtmlTableCell(); HtmlTableCell statuses = new HtmlTableCell(); HtmlTableCell followings = new HtmlTableCell(); HtmlTableCell followers = new HtmlTableCell(); HtmlTableCell delete = new HtmlTableCell(); username.InnerText = item.username; email.InnerText = item.email; HtmlImage img = new HtmlImage(); img.Alt = item.active.ToString(); img.Src = item.active ? "Images/yes.png" : "Images/no.png"; active.Attributes.Add("class", "center"); active.Controls.Add(img); statuses.Attributes.Add("class", "center"); Stopwatch w1 = Stopwatch.StartNew(); statuses.InnerText = db.Posts.Where(p => p.ChosenFeature.user == item.id).Count().ToString(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", count the number of posts of an user"); followings.Attributes.Add("class", "center"); Stopwatch w2 = Stopwatch.StartNew(); followings.InnerText = db.StaticFriends.Where(sf => sf.User == item).Count().ToString(); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", count the number of static friend of an user"); followers.Attributes.Add("class", "center"); Stopwatch w3 = Stopwatch.StartNew(); followers.InnerText = db.StaticFriends.Where(sf => sf.Friend == item).Count().ToString(); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", count the number of users friend of an user"); HtmlInputButton deleteBT = new HtmlInputButton(); deleteBT.Attributes.Add("title", "Delete " + item.username); deleteBT.Attributes.Add("class", "delete"); deleteBT.Value = item.id.ToString(); delete.Attributes.Add("class", "center"); delete.Controls.Add(deleteBT); HtmlTableRow tr = new HtmlTableRow(); tr.Cells.Add(username); tr.Cells.Add(email); tr.Cells.Add(active); tr.Cells.Add(statuses); tr.Cells.Add(followings); tr.Cells.Add(followers); tr.Cells.Add(delete); UserTable.Rows.Add(tr); } Stopwatch w4 = Stopwatch.StartNew(); var alphabet = (from usr in db.Users where !usr.isAdmin orderby usr.username group usr by usr.username.ToUpper().Substring(0, 1) into userGroup select new { firstLetter = userGroup.Key, user = userGroup }) .ToDictionary(firstLetter => firstLetter.firstLetter, firstLetter => firstLetter.user.Count()); w4.Stop(); ILog log4 = LogManager.GetLogger("QueryLogger"); log4.Info(" Elapsed time: " + w4.Elapsed + ", select a dictionary of first letters from users"); int sum = 0; for (char c = 'A'; c <= 'Z'; c++) { HtmlTableCell letter = new HtmlTableCell(); if (alphabet.Keys.Contains(c.ToString())) { HtmlInputButton but = new HtmlInputButton(); but.Attributes.Add("title", "Go to page where users start with " + c.ToString()); but.Value = c.ToString(); but.ID = ((sum / userPerPage) + 1).ToString(); but.Attributes.Add("class", "letters"); letter.Controls.Add(but); sum += alphabet[c.ToString()]; } else { letter.InnerText = c.ToString(); } AlphabetRow.Cells.Add(letter); } for (int i = 0; i <= (sum-1) / userPerPage; i++) { HtmlTableCell pagenum = new HtmlTableCell(); HtmlInputButton but = new HtmlInputButton(); but.Attributes.Add("title", "Go to page " + (i + 1).ToString()); but.Value = (i + 1).ToString(); but.ID = (i + 1).ToString(); if((i+1) == page) but.Attributes.Add("class", "highlightedpages"); else but.Attributes.Add("class", "pages"); pagenum.Controls.Add(but); PageRow.Cells.Add(pagenum); } }
public bool Post(String username, String password, String message) { Contract.Requires(!String.IsNullOrEmpty(username)); Contract.Requires(!String.IsNullOrEmpty(password)); Contract.Requires(!String.IsNullOrEmpty(message)); ConnectorDataContext db = new ConnectorDataContext(); User user = CheckCredentials(db, username, password); if (user == null) return false; ILog log = LogManager.GetLogger("PanelLogger"); log.Info(user.id + ",P"); Stopwatch w1 = Stopwatch.StartNew(); int service = db.ServiceInstances.Where(si => si.Service.name == "SocialTFS").Single().id; w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", select service instance's id of the service 'SocialTFS'"); long chosenFeature = -1; try { Stopwatch w2 = Stopwatch.StartNew(); chosenFeature = db.ChosenFeatures.Where(cf => cf.user == user.id && cf.serviceInstance == service && cf.feature == FeaturesType.Post.ToString()).First().id; w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", user id: " + user.id + ", service instance: " + service + ", feature's name: " + FeaturesType.Post.ToString() + ", select chosen feature's id"); } catch (InvalidOperationException) { try { Stopwatch w3 = Stopwatch.StartNew(); db.Registrations.Where(r => r.user == user.id && r.serviceInstance == service).Single(); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", user id: " + user.id + ", service instance: " + service + ", select registration of a service"); } catch { Registration registration = new Registration() { User = user, serviceInstance = db.ServiceInstances.Where(si => si.Service.name == "SocialTFS").Single().id, //considerata poco sopra per il log nameOnService = username, idOnService = username }; Stopwatch w4 = Stopwatch.StartNew(); db.Registrations.InsertOnSubmit(registration); db.SubmitChanges(); w4.Stop(); ILog log4 = LogManager.GetLogger("QueryLogger"); log4.Info(" Elapsed time: " + w4.Elapsed + ", insert a registration"); } ChosenFeature newChoseFeature = new ChosenFeature() { Registration = db.Registrations.Where(r => r.user == user.id && r.serviceInstance == service).Single(), //considerata poco sopra per il log feature = FeaturesType.Post.ToString(), lastDownload = new DateTime(1900, 1, 1) }; Stopwatch w5 = Stopwatch.StartNew(); db.ChosenFeatures.InsertOnSubmit(newChoseFeature); db.SubmitChanges(); w5.Stop(); ILog log5 = LogManager.GetLogger("QueryLogger"); log5.Info(" Elapsed time: " + w5.Elapsed + ", feature's name: " + FeaturesType.Post.ToString() + ", last download: " + new DateTime(1900, 1, 1) + ", insert a new chosen feature"); chosenFeature = newChoseFeature.id; } Stopwatch w6 = Stopwatch.StartNew(); db.Posts.InsertOnSubmit(new Post { chosenFeature = chosenFeature, message = message, createAt = DateTime.UtcNow }); db.SubmitChanges(); w6.Stop(); ILog log6 = LogManager.GetLogger("QueryLogger"); log6.Info(" Elapsed time: " + w6.Elapsed + ", message: " + message + ", date time: " + DateTime.UtcNow + ", insert the post"); return true; }
/// <summary> /// Convert an User (used for the database) in a WUser (used for the web). /// </summary> /// <param name="db">Database connector data context.</param> /// <param name="user">User that requires the conversion.</param> /// <param name="userToConvert">The User to convert.</param> /// <param name="calculateInfos">True if you need to have all the information about the User, false otherwise.</param> /// <returns>A WUser.</returns> public static WUser UserToWUser(ConnectorDataContext db, User user, User userToConvert, bool calculateInfos) { WUser result = null; if (calculateInfos) { Stopwatch w = Stopwatch.StartNew(); int stat = db.Posts.Where(p => p.ChosenFeature.user == userToConvert.id).Count(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", user id: " + userToConvert.id + ", count the number of posts of an user for a certain chosen feature"); Stopwatch w1 = Stopwatch.StartNew(); int followings = db.StaticFriends.Where(sf => sf.User == userToConvert).Count(); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", count the number of static friends of an user"); Stopwatch w2 = Stopwatch.StartNew(); int followers = db.StaticFriends.Where(sf => sf.Friend == userToConvert).Count(); w2.Stop(); ILog log2 = LogManager.GetLogger("QueryLogger"); log2.Info(" Elapsed time: " + w2.Elapsed + ", count the number of users that are static friends of an user"); Stopwatch w3 = Stopwatch.StartNew(); int followed = db.StaticFriends.Where(sf => sf.User == user && sf.Friend == userToConvert).Count(); w3.Stop(); ILog log3 = LogManager.GetLogger("QueryLogger"); log3.Info(" Elapsed time: " + w3.Elapsed + ", count the number of users that follow and are followed by an user"); result = new WUser() { Id = userToConvert.id, Username = userToConvert.username, Email = userToConvert.email, Avatar = userToConvert.avatar, Statuses = stat, Followings = followings, Followers = followers, Followed = followed == 1 }; } else { result = new WUser() { Id = userToConvert.id, Username = userToConvert.username, Email = userToConvert.email, Avatar = userToConvert.avatar, Statuses = -1, Followings = -1, Followers = -1, Followed = false }; } return result; }
private void CheckEmail(string email) { ConnectorDataContext db = new ConnectorDataContext(); Stopwatch w1 = Stopwatch.StartNew(); bool mail = db.Users.Any(u => u.email == email && !u.isAdmin); w1.Stop(); ILog log1 = LogManager.GetLogger("QueryLogger"); log1.Info(" Elapsed time: " + w1.Elapsed + ", check if the email is already used"); XDocument xml = new XDocument( new XElement("Root", new XElement("IsAviable", !mail))); Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xml); Response.End(); }
/// <summary> /// Convert a ServiceInstance (used for the database) in a WService (used for the web). /// </summary> /// <param name="db">Database connector data context.</param> /// <param name="user">User that requires the conversion.</param> /// <param name="serviceInstance">The ServiceInstance to convert.</param> /// <param name="calculateFeature">True if you need to have all the information about the User, false otherwise.</param> /// <returns>A WService.</returns> public static WService ServiceInstanceToWService(ConnectorDataContext db, User user, ServiceInstance serviceInstance, bool calculateFeature) { WService result = null; if (calculateFeature) { bool isRegistered = false; Stopwatch w = Stopwatch.StartNew(); IEnumerable<ServiceInstance> myServices = db.Registrations.Where(r => r.user == user.id).Select(r => r.ServiceInstance); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", user id: " + user.id + ", select all service instances of an user"); if (myServices.Contains(serviceInstance)) isRegistered = true; List<FeaturesType> privateFeatures = ServiceFactory.getService(serviceInstance.Service.name).GetPrivateFeatures(); bool requireOAuth = false; int oauthVersion = 0; if (privateFeatures.Contains(FeaturesType.OAuth1)) { requireOAuth = true; oauthVersion = 1; } else if (privateFeatures.Contains(FeaturesType.OAuth2)) { requireOAuth = true; oauthVersion = 2; } bool requireTFSAuthentication = false; bool requireTFSDomain = false; if (privateFeatures.Contains(FeaturesType.TFSAuthenticationWithDomain)) { requireTFSAuthentication = true; requireTFSDomain = true; } else if (privateFeatures.Contains(FeaturesType.TFSAuthenticationWithoutDomain)) { requireTFSAuthentication = true; requireTFSDomain = false; } result = new WService() { Id = serviceInstance.id, Name = serviceInstance.name, Host = serviceInstance.host, BaseService = serviceInstance.Service.name, Image = serviceInstance.Service.image, Registered = isRegistered, RequireOAuth = requireOAuth, OAuthVersion = oauthVersion, RequireTFSAuthentication = requireTFSAuthentication, RequireTFSDomain = requireTFSDomain }; } else { result = new WService() { Id = serviceInstance.id, Name = serviceInstance.name, BaseService = serviceInstance.Service.name, Image = serviceInstance.Service.image }; } return result; }
public bool Authorize(string username, string password, int service, string verifier, string accessToken, string accessSecret) { Contract.Requires(!String.IsNullOrEmpty(username)); Contract.Requires(!String.IsNullOrEmpty(password)); Contract.Requires(!String.IsNullOrEmpty(accessToken)); ConnectorDataContext db = new ConnectorDataContext(); User user = CheckCredentials(db, username, password); if (user == null) return false; Stopwatch w = Stopwatch.StartNew(); ServiceInstance si = db.ServiceInstances.Where(s => s.id == service).Single(); w.Stop(); ILog log = LogManager.GetLogger("QueryLogger"); log.Info(" Elapsed time: " + w.Elapsed + ", service id: " + service + ", select service id from serviceinstance"); IService iService = ServiceFactory.getService(si.Service.name); if (iService.GetPrivateFeatures().Contains(FeaturesType.OAuth1)) { iService = ServiceFactory.getServiceOauth(si.Service.name, si.host, si.consumerKey, si.consumerSecret, accessToken, accessSecret); OAuthAccessData oauthData = iService.Get(FeaturesType.OAuth1, OAuth1Phase.Authorize, si.host + si.Service.accessToken, verifier) as OAuthAccessData; if (oauthData == null) return false; IUser iUser = iService.VerifyCredential(); return RegisterUserOnAService(db, user, si, iUser, oauthData.AccessToken, oauthData.AccessSecret); } else if (iService.GetPrivateFeatures().Contains(FeaturesType.OAuth2)) { if (si.Service.name.Equals("GitHub") || si.Service.name.Equals("LinkedIn")) { accessToken = iService.Get(FeaturesType.OAuth2, si.Service.name, si.host, si.consumerKey, si.consumerSecret, accessToken) as string; } iService = ServiceFactory.getServiceOauth(si.Service.name, si.host, si.consumerKey, si.consumerSecret, accessToken, null); IUser iUser = iService.VerifyCredential(); return RegisterUserOnAService(db, user, si, iUser, accessToken, null); } return false; }