Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Joona Romppanen
/
Moya Info Tools
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit f78ff2de
authored
Jun 07, 2017
by
Joona Romppanen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
UserLoginControl cleanup & better exception handler
1 parent
9f01e3a2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
117 additions
and
90 deletions
MoyaAdmin/MoyaAdminUI/MoyaAdminLib/MoyaApiException.cs
MoyaAdmin/MoyaAdminUI/MoyaAdminLib/RestClient.cs
MoyaSignup/UserLoginControl.Designer.cs
MoyaSignup/UserLoginControl.cs
MoyaAdmin/MoyaAdminUI/MoyaAdminLib/MoyaApiException.cs
View file @
f78ff2d
...
...
@@ -5,16 +5,20 @@ namespace MoyaAdminLib
{
public
class
MoyaApiException
:
WebException
{
public
HttpStatusCode
StatusCode
{
get
;
set
;
}
public
MoyaApiException
()
{
}
public
MoyaApiException
(
string
message
)
:
base
(
message
)
public
MoyaApiException
(
string
message
,
HttpStatusCode
statusCode
)
:
base
(
message
)
{
StatusCode
=
statusCode
;
}
public
MoyaApiException
(
string
message
,
Exception
innerException
)
:
base
(
message
,
innerException
)
public
MoyaApiException
(
string
message
,
HttpStatusCode
statusCode
,
Exception
innerException
)
:
base
(
message
,
innerException
)
{
StatusCode
=
statusCode
;
}
}
}
MoyaAdmin/MoyaAdminUI/MoyaAdminLib/RestClient.cs
View file @
f78ff2d
...
...
@@ -141,8 +141,7 @@ namespace MoyaAdminLib
if
((
int
)
response
.
StatusCode
<
200
&&
(
int
)
response
.
StatusCode
>=
300
)
{
var
message
=
string
.
Format
(
"Request failed. Received HTTP {0}"
,
response
.
StatusCode
);
throw
new
ApplicationException
(
message
);
throw
new
MoyaApiException
(
"Request failed"
,
response
.
StatusCode
);
}
using
(
var
responseStream
=
response
.
GetResponseStream
())
...
...
@@ -171,7 +170,7 @@ namespace MoyaAdminLib
{
var
responseValue
=
StreamToString
(
responseStream
);
Console
.
WriteLine
(
"Response was "
+
responseValue
);
throw
new
MoyaApi
Exception
(
responseValue
,
e
);
throw
new
Web
Exception
(
responseValue
,
e
);
}
...
...
MoyaSignup/UserLoginControl.Designer.cs
View file @
f78ff2d
...
...
@@ -60,13 +60,13 @@
this
.
txtEmail
.
Name
=
"txtEmail"
;
this
.
txtEmail
.
Size
=
new
System
.
Drawing
.
Size
(
395
,
26
);
this
.
txtEmail
.
TabIndex
=
67
;
this
.
txtEmail
.
TextChanged
+=
new
System
.
EventHandler
(
this
.
t
xtEmail_TextChanged
);
this
.
txtEmail
.
KeyUp
+=
new
System
.
Windows
.
Forms
.
KeyEventHandler
(
this
.
t
xtEmail_KeyUp
);
this
.
txtEmail
.
TextChanged
+=
new
System
.
EventHandler
(
this
.
T
xtEmail_TextChanged
);
this
.
txtEmail
.
KeyUp
+=
new
System
.
Windows
.
Forms
.
KeyEventHandler
(
this
.
T
xtEmail_KeyUp
);
//
// timer1
//
this
.
timer1
.
Interval
=
500
;
this
.
timer1
.
Tick
+=
new
System
.
EventHandler
(
this
.
t
imer1_Tick
);
this
.
timer1
.
Tick
+=
new
System
.
EventHandler
(
this
.
T
imer1_Tick
);
//
// infoLabel
//
...
...
@@ -89,7 +89,7 @@
this
.
passwordTextBox
.
Size
=
new
System
.
Drawing
.
Size
(
125
,
26
);
this
.
passwordTextBox
.
TabIndex
=
93
;
this
.
passwordTextBox
.
Visible
=
false
;
this
.
passwordTextBox
.
KeyDown
+=
new
System
.
Windows
.
Forms
.
KeyEventHandler
(
this
.
p
asswordTextBox_KeyDown
);
this
.
passwordTextBox
.
KeyDown
+=
new
System
.
Windows
.
Forms
.
KeyEventHandler
(
this
.
P
asswordTextBox_KeyDown
);
//
// loginButton
//
...
...
@@ -100,7 +100,7 @@
this
.
loginButton
.
Text
=
"Kirjaudu tapahtumaan"
;
this
.
loginButton
.
UseVisualStyleBackColor
=
true
;
this
.
loginButton
.
Visible
=
false
;
this
.
loginButton
.
Click
+=
new
System
.
EventHandler
(
this
.
l
oginButton_Click
);
this
.
loginButton
.
Click
+=
new
System
.
EventHandler
(
this
.
L
oginButton_Click
);
//
// forgottenPasswordButton
//
...
...
@@ -121,7 +121,7 @@
this
.
btnNewProfile
.
Text
=
"Lisää omat tiedot"
;
this
.
btnNewProfile
.
UseVisualStyleBackColor
=
true
;
this
.
btnNewProfile
.
Visible
=
false
;
this
.
btnNewProfile
.
Click
+=
new
System
.
EventHandler
(
this
.
b
tnNewProfile_Click
);
this
.
btnNewProfile
.
Click
+=
new
System
.
EventHandler
(
this
.
B
tnNewProfile_Click
);
//
// UserLoginControl
//
...
...
MoyaSignup/UserLoginControl.cs
View file @
f78ff2d
using
System
;
using
System.Collections.Generic
;
using
System.ComponentModel
;
using
System.Diagnostics
;
using
System.Drawing
;
using
System.Data
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
System.Windows.Forms
;
using
MoyaAdminLib
;
using
System.Web.Script.Serialization
;
...
...
@@ -17,25 +12,27 @@ namespace MoyaSignup
public
partial
class
UserLoginControl
:
UserControl
{
public
event
EventHandler
LoginOk
;
public
delegate
void
ThermoPrinterEvent
(
ThermoPrinter
.
Models
model
);
public
event
ThermoPrinterEvent
SetPrinter
;
public
event
ThermoPrinterEvent
SetPrinter
;
public
User
CurrentUser
;
public
UserLoginControl
()
{
InitializeComponent
();
SetStyle
(
ControlStyles
.
SupportsTransparentBackColor
,
true
);
this
.
BackColor
=
Color
.
Transparent
;
BackColor
=
Color
.
Transparent
;
infoLabel
.
Text
=
"Hei! Kirjoita sähköpostiosoitteesi"
;
}
private
void
UserLoginControl_Load
(
object
sender
,
EventArgs
e
)
{
}
private
void
t
xtEmail_TextChanged
(
object
sender
,
EventArgs
e
)
private
void
T
xtEmail_TextChanged
(
object
sender
,
EventArgs
e
)
{
string
txt
=
txtEmail
.
Text
;
var
txt
=
txtEmail
.
Text
;
if
(
IsValidEmail
(
txt
))
{
timer1
.
Stop
();
...
...
@@ -45,14 +42,12 @@ namespace MoyaSignup
{
if
(
txtEmail
.
Text
==
"thermo2"
)
{
if
(
SetPrinter
!=
null
)
SetPrinter
(
ThermoPrinter
.
Models
.
IDP3210
);
SetPrinter
?.
Invoke
(
ThermoPrinter
.
Models
.
IDP3210
);
txtEmail
.
Text
=
"OK"
;
}
else
if
(
txtEmail
.
Text
==
"thermo1"
)
{
if
(
SetPrinter
!=
null
)
SetPrinter
(
ThermoPrinterLibrary
.
ThermoPrinter
.
Models
.
TMT188II
);
SetPrinter
?.
Invoke
(
ThermoPrinter
.
Models
.
TMT188II
);
txtEmail
.
Text
=
"OK"
;
}
...
...
@@ -63,25 +58,35 @@ namespace MoyaSignup
}
}
bool
IsValidEmail
(
string
email
)
private
static
bool
IsValidEmail
(
string
email
)
{
if
(!
email
.
Contains
(
"@"
)
||
!
email
.
Contains
(
"."
))
if
(!
email
.
Contains
(
"@"
)
||
!
email
.
Contains
(
"."
))
{
return
false
;
string
[]
parts
=
email
.
Split
(
new
string
[]
{
"@"
},
StringSplitOptions
.
RemoveEmptyEntries
);
if
(
parts
.
Count
<
string
>()
==
2
)
}
var
parts
=
email
.
Split
(
new
[]
{
"@"
},
StringSplitOptions
.
RemoveEmptyEntries
);
if
(
parts
.
Length
==
2
)
{
if
(!
parts
[
1
].
Contains
(
"."
))
{
return
false
;
string
[]
domainparts
=
parts
[
1
].
Split
(
new
string
[]
{
"."
},
StringSplitOptions
.
RemoveEmptyEntries
);
if
(
domainparts
.
Count
<
string
>()
<
2
)
}
var
domainparts
=
parts
[
1
].
Split
(
new
[]
{
"."
},
StringSplitOptions
.
RemoveEmptyEntries
);
if
(
domainparts
.
Length
<
2
)
{
return
false
;
string
ltd
=
domainparts
[
domainparts
.
Count
<
string
>()
-
1
];
if
(
ltd
.
Length
<
2
)
}
var
ltd
=
domainparts
[
domainparts
.
Length
-
1
];
if
(
ltd
.
Length
<
2
)
{
return
false
;
}
}
else
{
return
false
;
}
else
return
false
;
try
{
...
...
@@ -93,102 +98,121 @@ namespace MoyaSignup
return
false
;
}
}
public
User
CurrentUser
;
private
void
t
imer1_Tick
(
object
sender
,
EventArgs
e
)
private
void
T
imer1_Tick
(
object
sender
,
EventArgs
e
)
{
RestClient
client
=
new
RestClient
();
var
client
=
new
RestClient
();
try
{
string
json
=
client
.
MakeRequest
(
"user"
,
"email="
+
txtEmail
.
Text
);
JavaScriptSerialize
r
ser
=
new
JavaScriptSerializer
();
Eventuse
r
euser
=
ser
.
Deserialize
<
Eventuser
>(
json
);
var
json
=
client
.
MakeRequest
(
"user"
,
"email="
+
txtEmail
.
Text
);
va
r
ser
=
new
JavaScriptSerializer
();
va
r
euser
=
ser
.
Deserialize
<
Eventuser
>(
json
);
if
(
euser
!=
null
)
{
btnNewProfile
.
Visible
=
false
;
CurrentUser
=
new
User
(
euser
);
infoLabel
.
Text
=
"Hei! "
+
CurrentUser
+
" Kirjoita salasanasi ja kirjaudu"
;
s
howLogin
(
true
);
S
howLogin
(
true
);
}
}
catch
(
MoyaApiException
)
catch
(
WebException
ex
)
{
CurrentUser
=
null
;
infoLabel
.
Text
=
"Uusi kävijä?"
;
btnNewProfile
.
Visible
=
true
;
showLogin
(
false
);
if
(
ex
is
MoyaApiException
&&
((
MoyaApiException
)
ex
).
StatusCode
==
HttpStatusCode
.
NotFound
)
{
CurrentUser
=
null
;
infoLabel
.
Text
=
"Uusi kävijä?"
;
btnNewProfile
.
Visible
=
true
;
ShowLogin
(
false
);
}
else
{
MessageBox
.
Show
(
"Yhteys taustajärjestelmään epäonnistui. Ota yhteys infoon."
,
"Järjestelmävirhe"
,
MessageBoxButtons
.
OK
,
MessageBoxIcon
.
Error
);
}
}
catch
(
WebException
)
catch
(
Exception
ex
)
{
using
(
var
eventLog
=
new
EventLog
(
"Application"
))
{
eventLog
.
Source
=
"MoyaSignup"
;
eventLog
.
WriteEntry
(
string
.
Format
(
"Odottamaton virhe:\n{0}"
,
ex
.
StackTrace
),
EventLogEntryType
.
Error
,
666
);
}
MessageBox
.
Show
(
"
Yhteys taustajärjestelmään epäonnistui. Ota yhteys infoon."
,
"Järjestelmävirhe"
,
MessageBoxButtons
.
OK
,
"
Odottamaton virhe. Ota yhteys infoon."
,
"Järjestelmävirhe"
,
MessageBoxButtons
.
OK
,
MessageBoxIcon
.
Error
);
}
timer1
.
Stop
();
}
void
s
howLogin
(
bool
show
)
private
void
S
howLogin
(
bool
show
)
{
passwordTextBox
.
Visible
=
show
;
loginButton
.
Visible
=
show
;
}
private
void
login
()
{
RestClient
client
;
client
=
new
RestClient
(
RestClient
.
ApiURL
,
HttpVerb
.
POST
);
private
void
Login
()
{
var
client
=
new
RestClient
(
RestClient
.
ApiURL
,
HttpVerb
.
POST
);
//client.PostData = "password=" + WebUtility.UrlEncode(passwordTextBox.Text);
client
.
PostData
=
"password="
+
passwordTextBox
.
Text
;
client
.
ContentType
=
"application/x-www-form-urlencoded"
;
try
{
string
json
=
client
.
MakeRequest
(
"v2/user/"
+
CurrentUser
.
UserId
+
"/check-password"
);
//string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password");
//
/
MoyaWeb/rest/user/eventusers
JavaScriptSerialize
r
ser
=
new
JavaScriptSerializer
();
Eventuse
r
euser
=
ser
.
Deserialize
<
Eventuser
>(
json
);
var
json
=
client
.
MakeRequest
(
"v2/user/"
+
CurrentUser
.
UserId
+
"/check-password"
);
//
string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password");
//
MoyaWeb/rest/user/eventusers
va
r
ser
=
new
JavaScriptSerializer
();
va
r
euser
=
ser
.
Deserialize
<
Eventuser
>(
json
);
if
(
euser
!=
null
)
{
CurrentUser
=
new
User
(
euser
);
CurrentUser
.
Email
=
txtEmail
.
Text
;
if
(
LoginOk
!=
null
)
LoginOk
(
this
,
null
);
CurrentUser
=
new
User
(
euser
)
{
Email
=
txtEmail
.
Text
};
LoginOk
?.
Invoke
(
this
,
null
);
}
else
wrongPass
();
WrongPass
();
}
catch
(
Exception
ex
)
catch
(
Exception
)
{
w
rongPass
();
W
rongPass
();
}
}
private
void
loginButton_Click
(
object
sender
,
EventArgs
e
)
private
void
LoginButton_Click
(
object
sender
,
EventArgs
e
)
{
l
ogin
();
L
ogin
();
}
private
void
wrongPass
()
private
void
WrongPass
()
{
infoLabel
.
Text
=
"Väärä salasana. Syötä salasana uudelleen"
;
}
private
void
p
asswordTextBox_KeyDown
(
object
sender
,
KeyEventArgs
e
)
private
void
P
asswordTextBox_KeyDown
(
object
sender
,
KeyEventArgs
e
)
{
if
(
e
.
KeyData
==
Keys
.
Enter
)
l
ogin
();
L
ogin
();
}
private
void
b
tnNewProfile_Click
(
object
sender
,
EventArgs
e
)
private
void
B
tnNewProfile_Click
(
object
sender
,
EventArgs
e
)
{
CurrentUser
=
new
User
();
CurrentUser
.
Email
=
txtEmail
.
Text
;
CurrentUser
=
new
User
{
Email
=
txtEmail
.
Text
};
//Send info to parent control
if
(
LoginOk
!=
null
)
LoginOk
(
this
,
null
);
LoginOk
?.
Invoke
(
this
,
null
);
}
...
...
@@ -199,7 +223,7 @@ namespace MoyaSignup
passwordTextBox
.
Visible
=
false
;
passwordTextBox
.
Text
=
""
;
SetStyle
(
ControlStyles
.
SupportsTransparentBackColor
,
true
);
this
.
BackColor
=
Color
.
Transparent
;
BackColor
=
Color
.
Transparent
;
infoLabel
.
Text
=
"Hei! Kirjoita sähköpostiosoitteesi"
;
loginButton
.
Visible
=
false
;
}
...
...
@@ -211,15 +235,15 @@ namespace MoyaSignup
Application
.
Exit
();
return
;
}
else
if
(
e
.
Alt
&&
e
.
KeyCode
==
Keys
.
F5
)
if
(
e
.
Alt
&&
e
.
KeyCode
==
Keys
.
F5
)
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210");
}
}
private
void
t
xtEmail_KeyUp
(
object
sender
,
KeyEventArgs
e
)
private
void
T
xtEmail_KeyUp
(
object
sender
,
KeyEventArgs
e
)
{
if
(
e
.
Alt
&&
e
.
Control
&&
e
.
KeyCode
==
Keys
.
S
)
{
...
...
@@ -227,12 +251,11 @@ namespace MoyaSignup
Application
.
Exit
();
return
;
}
else
if
(
e
.
Alt
&&
e
.
KeyCode
==
Keys
.
F5
)
if
(
e
.
Alt
&&
e
.
KeyCode
==
Keys
.
F5
)
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210");
}
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment