PageRenderTime 61ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 16ms app.codeStats 0ms

/Database/Database.cs

https://github.com/cmath/SimpleLoginManager
C# | 84 lines | 71 code | 12 blank | 1 comment | 12 complexity | 07712f1c17322cb33feaf4f90dd17f2d MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel.Composition;
 4using System.Linq;
 5using System.Text;
 6using System.Threading.Tasks;
 7using System.Xml.Linq;
 8
 9namespace Login.Database
10{
11    [Export(typeof(IDatabase)), Export]
12    public class Database : IDatabase
13    {
14        XDocument doc_;
15
16        public Database ( )
17        {
18            doc_ = XDocument.Load("Database.xml");
19        }
20
21        private void Save()
22        {
23            doc_.Save("Database.xml");
24        }
25
26        private XElement MakeQuery ( string Login )
27        {
28            var query = from user in doc_.Element("Root").Elements("User")
29                        where ( string )user.Element("Login") == Login
30                        select user;
31
32            if ( query.Count( ) == 1 )
33            {
34                return query.First( );
35            }
36
37            return null;
38        }
39        public int GetNumberOfLoginTries ( string Login )
40        {
41
42            //var ref_how_it_works = doc_.Element("Root").Elements("User").Single(x => ( string )x.Element("Login") == Login);
43            var query = MakeQuery(Login);
44            if ( query != null )
45            {
46                return (int)query.Element("Tries");
47            }
48
49            return -1;
50        }
51
52        public string GetAllowedIp ( string Login )
53        {
54            var query = MakeQuery(Login);
55            if ( query != null )
56            {
57                return ( string )query.Element("IpAddress");
58            }
59
60            return "";
61        }
62
63        public bool ValidateLoginAndPassword ( string Login, string Password )
64        {
65            var query = MakeQuery(Login);
66            if ( query != null )
67            {
68                return Password == (string)query.Element("Password");
69            }
70            return false;
71        }
72
73        public void UpdateNumberOfLoginTries ( string Login )
74        {
75            var query = MakeQuery(Login);
76            if ( query != null )
77            {
78                int current_number = (int)query.Element("Tries");
79                query.SetElementValue("Tries", ++current_number);
80                this.Save( );
81            }
82        }
83    }
84}